开发者

regular expression requires something in front but does not include that in the match

I want a regex that will generate a match when it encounters "integer xyz" but only returns 'xyz' as the matchi开发者_运维百科ng text. 'xyz' can be an identifier (letters+digits+underscore).

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

thanks!


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.


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.


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]+)


/integer\s+([a-zA-Z0-9_]+)/

then get the backreference \1


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜