RegExp matching string not starting with my
Asked Answered
W

4

152

For PMD I'd like to have a rule which warns me of those ugly variables which start with my.
This means I have to accept all variables which do NOT start with my.

So, I need a RegEx (re) which behaves as follows:

re.match('myVar')       == false
re.match('manager')     == true
re.match('thisIsMyVar') == true
re.match('myOtherVar')  == false
re.match('stuff')       == true

I've tried different ones but haven't got it working yet.

Willamina answered 22/1, 2010 at 9:44 Comment(1)
I think that actually should be a negative look behind (not a negative look ahead).Gibbsite
I
53

You could either use a lookahead assertion like others have suggested. Or, if you just want to use basic regular expression syntax:

^(.?$|[^m].+|m[^y].*)

This matches strings that are either zero or one characters long (^.?$) and thus can not be my. Or strings with two or more characters where when the first character is not an m any more characters may follow (^[^m].+); or if the first character is a m it must not be followed by a y (^m[^y]).

Intertwist answered 22/1, 2010 at 10:9 Comment(1)
This is the one we could use now. Seems like there is a problem with the extended regexp, but this one works fine for now.Willamina
D
250
^(?!my)\w+$

should work.

It first ensures that it's not possible to match my at the start of the string, and then matches alphanumeric characters until the end of the string. Whitespace anywhere in the string will cause the regex to fail. Depending on your input you might want to either strip whitespace in the front and back of the string before passing it to the regex, or use add optional whitespace matchers to the regex like ^\s*(?!my)(\w+)\s*$. In this case, backreference 1 will contain the name of the variable.

And if you need to ensure that your variable name starts with a certain group of characters, say [A-Za-z_], use

^(?!my)[A-Za-z_]\w*$

Note the change from + to *.

Disbranch answered 22/1, 2010 at 9:48 Comment(0)
T
90
/^(?!my).*/

(?!expression) is a negative lookahead; it matches a position where expression doesn't match starting at that position.

Turtleneck answered 22/1, 2010 at 9:49 Comment(0)
I
53

You could either use a lookahead assertion like others have suggested. Or, if you just want to use basic regular expression syntax:

^(.?$|[^m].+|m[^y].*)

This matches strings that are either zero or one characters long (^.?$) and thus can not be my. Or strings with two or more characters where when the first character is not an m any more characters may follow (^[^m].+); or if the first character is a m it must not be followed by a y (^m[^y]).

Intertwist answered 22/1, 2010 at 10:9 Comment(1)
This is the one we could use now. Seems like there is a problem with the extended regexp, but this one works fine for now.Willamina
U
9

Wouldn't it be significantly more readable to do a positive match and reject those strings - rather than match the negative to find strings to accept?

/^my/
Unfailing answered 12/9, 2011 at 16:34 Comment(1)
That would be easier BUT it (was) needed for a rule which checks the code when commiting to a repository, hence the logic could not be "reverted".Willamina

© 2022 - 2024 — McMap. All rights reserved.