开发者

regex problem - select results by id

Here's my regex problem: How can I select all results with a certain ID, e.g. "...ID=99" but excluding the results countinuing with an additional number like ID="990" or "ID=9923". However if the string countinues with another non-number character ("&"), e.g. "...ID=99&PARAM=9290" it also should be included.

I am totally confused turning this into a regex selection. I wou开发者_JAVA百科ld appreciate any idea on this very much!

(by the way if you are really into regex. how did you learn it? any recommended resources, books, tutorials?)

Note: I use this to filter my search results in google analytics as you can actually use regex in the "Filter Page" form. Maybe this is useful information to you.


Use \b, word boundary, i.e.

ID=99\b

For books Regular Expression Recipes: A Problem-Solution Approach is a good one.


Answering the second part of your question:

As far as books are concerned, Regular Expression Cookbook by O'Reilly is fantastic. The first few chapters pretty much cover all the basics, and then some. The rest of the book is concrete examples.


ID=99(?!\d)

(?!\d) is a negative lookahead; it asserts that either the next character is not a digit or there is no next character. You didn't say what regex flavor you're using, but most of the so-called Perl-compatible flavors support lookaheads.

As for learning resources, the tutorial at regular-expressions.info is a great place to start. For advanced study, the Goyvaerts-Levithan book recommended by others is excellent, but Mastering Regular Expressions is still the best. Get both if you can afford them; you won't regret it.

EDIT: To be on the safe side, you might want to use \bID=99(?!\d) to avoid matching something like FOO_ID=99.


This will match ID=99 and ID=99 followed by a single non-digit char.

ID=99(:?\D)?

If 99 was just an example and you want to match any two digits, you can use:

ID=\d{2}(:?\D)?
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜