开发者

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 se开发者_C百科lects the begin name. How can I change it to not include begin?


(?!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.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜