tags:

views:

48

answers:

5

I want a regex that will generate a match when it encounters "integer xyz" but only returns 'xyz' as the matching text. 'xyz' can be an identifier (letters+digits+underscore).

xyz by itself generates no match, only xyz preceded by 'integer '

thanks!

+1  A: 

That's not possible. If you want it to match xyz in "integer xyz" but not in "just xyz", then you are really looking for a match of "integer xyz".

What you probably need to do is expect that the match is going to be "integer xyz" and then clean up the match to extract the "xyz" portion.

mlathe
+1  A: 

It depends in what programing language you use. You will need a regex with a group, the second braces () contains the variable name.

integer\s+([^,;\s]+)
Am
A: 
/integer\s+([a-zA-Z0-9_]+)/

then get the backreference \1

ghostdog74
A: 

Am I missing something or wouldn't you just put xyz in brackets, where xyz is what you care about (in your case looks like [a-zA-Z0-9_]+)?

so integer ([a-zA-Z0-9_]+)

which you then reference what is in the brackets with $1 or equivalent given your language

Pete
i think by xyz he means an unknown variable name. otherwise you are right..
Am
thanks, updated xyz to be more generic
Pete
+1  A: 

What language? In Perl 5.10 or PHP 5, I'd use \K:

/integer \K\w+/

For most other languages I'd suggest a lookbehind:

(?<=integer )\w+

If it's JavaScript, I'd recommend becoming a reader of Steve Levithan's blog. Come to think of it, I'd recommend that in any case. :-)

Of course, that's assuming the regex must match only the variable name. Otherwise I'd go with the other responders' recommendation of using a capturing group.

Alan Moore