• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex lookahead question  RSS feed

 
David Lindahl
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is basically a general regex question tho I'm using the Java-flavor of it.
I'm parsing a file containing variables wich are denoted by $name. I need to find all the variables for later use. However one syntax gives me a problem:
$varName in $varList
Here I only wish to match $varList. I've tried to use a negative lookahead like so:

The only effect this has is, quite logicaly, that the last w+ before a " in" isnt matched, ie my matches would be $varNam and $varList. How do I solve this? Theres probably some stupidly easy way but I just cant see it!
Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One option is to add a "word boundary" to the regex. This will force the match to match the whole word, and not allow it to backup one letter to avoid the "in" word.

Henry
 
David Lindahl
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very simple and working solution, thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!