jduncanator
jduncanator

Reputation: 2194

Greedy and non-greedy regex

I currently have this regex: this\.(.*)?\s[=,]\s, however I have come across a pickle I cannot fix.

I tried the following Regex, which works, but it captures the space as well which I don't want: this\.(.*)?(?<=\s)=|(?<!\s),. What I'm trying to do is match identifier names. An example of what I want and the result is this:

this.""W = blah; which would match ""W. The second regex above does this almost perfectly, however it also captures the space before the = in the first group. Can someone point me in the correct direction to fix this?

EDIT: The reason for not simply using [^\s] in the wildcard group is that sometimes I can get lines like this: this. "$ = blah;

EDIT2: Now I have another issue. Its not matching lines like param1.readBytes(this.=!3,0,param1.readInt()); properly. Instead of matching =!3 its matching =!3,0. Is there a way to fix this? Again, I cannot simply use a [^,] because there could be a name like param1.readBytes(this.,3$,0,param1.readInt()); which should match ,3$.

Upvotes: 0

Views: 175

Answers (2)

Lefteris E
Lefteris E

Reputation: 2814

(.*) will match any character including whitespace. To force it not to end in whitespace change it to (.*[^\s])

Eg: this\.(.*[^\s])?\s?[=,]\s

For your second edit, it seems like you are doing a language parser. Even though regular expressions are powerful, they do have limits. You need a grammar parser for that.

Upvotes: 2

Didier Trosset
Didier Trosset

Reputation: 37477

Maybe you can tell in your first block to capture non space characters, instead of any.

this\.(\S*)?(?<=\s)=|(?<!\s),

Upvotes: 1

Related Questions