Reputation: 2194
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
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
Reputation: 37477
Maybe you can tell in your first block to capture non space characters, instead of any.
this\.(\S*)?(?<=\s)=|(?<!\s),
Upvotes: 1