Reputation: 1684
I'm in need of a regular expression that acts like the following:
matches (any part of foo()
in the following statement):
foo()
arg: foo()
foo()
(arg: foo()) {}
does not match:
@foo()
I currently have the following, but it has some problems:
^\s*?(?<!@)((\w+?)\()
^\s*?
includes any whitespace at the beginning of the line, which means arg: foo()
doesn't match the foo()
bit. I had to include this to get the @
lookbehind working correctly;(?<!@)
is a lookbehind to discard the match if a @
before the thing()
is matched;(\w+?)\(
matches the part of thething(
correctly, only if there's no @
before it.If there's no ^\s*?
in the regex, it would be behaving partly correct, but this shouldn't happen. It should rather discard the match entirely (not just for one character):
It has to discard the match entirely if any @
is before it, although it must match this correctly: @Mode foo()
(the foo()
bit, disregarding the @Mode
before it).
If there are any tips to help me out, that would be awesome!
Upvotes: 1
Views: 808
Reputation: 18641
Use
(?<![\w@])\w+\(\)
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
[\w@] any character of: word characters (a-z,
A-Z, 0-9, _), '@'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
\) ')'
Upvotes: 1