tags:

views:

111

answers:

5
+3  A: 

Use a regex like this one:

_id=([a-f0-9]+)&

The parenthesis define a group which you can retrieve from the results.

Drew Noakes
+4  A: 
id=([^&]*)&

The data between id= and & will be matched by the first (and only) group and then be accessible via .group(1) or similar depending on the language/regex library.

Edit: Changed + to * as per Johannes Rössel's suggestion.

sepp2k
Make the `+` a `*` to avoid strange results when id is empty under some weird circumstances.
Joey
A: 

I'd use

perl -ne 'm/[&?]_id=([^&]+)(&|$)/ && print $1;' [file]

where [file] is the name of the file containing the data.

nicerobot
+3  A: 
Brad Gilbert
good catch - the end-of-string would break everyone else's regexps. i'd still recommend using a cgi param parsing utility, since there's a good chance your language ships one with the stdlib and it'll have taken care of the edge cases.
Martin DeMello
+1. YOu have just convinced me that I need to practice my regexps. I would not have covered all your cases if I had written a similar regexp today.
Jørgen Fogh
Brad Gilbert
A: 

I'd use the following (non-greedy) Perl regex:

/_id=(.*?)&/
rk
Brad Gilbert
Could you please explain why that is the case? Is it because of the greedy vs non-greedy matching?
rk
Yes exactly. ​
Brad Gilbert