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

13

I have a file containing the following content:

Maringa=123123
Marialva=789789
Mandaguacu=123456
Cidadex=A341a2

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

15

You can capture regex groups after the match. Example:

(\w+)=(\w+)

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:

\w+(?==)

(?=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
13

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
2

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