Olympian
Olympian

Reputation: 768

How do I exclude a word from a regular expression search?

How I can create a regular expression for the following problem:

I have a string,
name1=value1;name2=value2;.....;

Somewhere, there exists a pair, "begin=10072011;" I need, with regular expressions, to parse from the string all name=value; pairs, where the value is a number. However, I want to ignore the name begin

Currently I have the following regexp:

([\\w]+)=([\\d]+);

Mine selects the begin name. How can I change it to not include begin?

Upvotes: 1

Views: 3100

Answers (3)

user850062
user850062

Reputation: 71

I think (?<=begin=)\d+(?=;) will be a better choice.

If you keep all the information in XML format, the work will be much easier than now.

Upvotes: 0

Alan Moore
Alan Moore

Reputation: 75242

This should do it:

\b(?!begin=)(\w+)=(\d+)\b

As aC++ string literal it would look like this:

"\\b(?!begin=)(\\w+)=(\\d+)\\b"

\b is a word boundary; you use it to make sure you're matching a whole word (as "word" is defined in the context of regexes; read that page carefully). For example, without the first \b the regex would correctly fail to match

begin=1234   // OK

...but then it would skip ahead one position and match:

egin=1234    // oops!

Upvotes: 1

Andrew Clark
Andrew Clark

Reputation: 208545

(?!begin)\b(\w+)=(\d+);

This uses negative lookahead, so it will not match if the string starts with "begin". The \b is necessary so that the regex does not just skip the "b" and match "egin=...".

Note that when describing a regex you should only using a single backslash for escapes, although for some languages you will need to use double backslashes to escape the backslash.

Upvotes: 5

Related Questions