Catch only the values before the "=" character using regular expression?


I have a file containing the following content:


How do I get only the characters before = , using regular expression?

I tried it like this: .*= But the String comes with the same char (=), I wish I could deny this char.

asked by anonymous 13.12.2013 / 17:46

3 answers


You can capture regex groups after the match. Example:


So the first group will be its identifier and the second will be the value.

If you still prefer to make a regex that only recognizes the identifier you can use:


(?=algo) is called a "positive lookahead". It is a way to look at the next text and confirm that it is algo . If it is not, fail. If it is, accept the regex, but do not include it in the result. There are other variants like the "negative lookahead" (?!algo) that does the opposite. Note that not every regular expression library supports this type of syntax.

13.12.2013 / 17:52

Use ^[^=]* , which will recognize an unbroken string of characters other than = at the beginning of the line. In parts:

  • ^ : recognizes the beginning of the line;
  • [^=] : recognizes a character other than = (at the beginning of an expression with square brackets, the caret, ^ , represents negation);
  • * : recognizes the previous expression ( [^=] ) as many times as possible.

Without the first part ( ^ ), the regular expression produces two results: the text before = and the text after = . If you are executing the regular expression on each line and taking only the first result, ^ of the start is optional and the expression can be simplified to [^=]* .

13.12.2013 / 17:51

Depending on what you want to do, it may be best to use split to break the string in two.

In javascript:

var linha     = "Maringa=123123";
var resultado = linha.split("=');

$resultado is now an array containing ['Maringa', '123123'] ;

01.02.2014 / 15:40