Regex for matching a word without a ( after it
-
11-10-2019 - |
Pergunta
What regex would check a line for a word that is not followed by a (
character? I tried (\w+)(\?!\()
but it doesn't work, and (\w+)[^\(]
matches anything by treating the last letter as the [^\(]
part. I am using the D programming language.
Examples of things that should match:
It should match the asdf
in the following:
asdf blah
asdf.beef
(asdf)
but not in these:
asdf(blah)
However, in asdf(blah)
, the blah
would be matched. Also in asdf blah
and asdf.beef
, the blah
and beef
would also be matched.
Solução
I don't know anything about D's capabilities. If it supports the perl regexp language, you can do this:
\w+\b(?!\()
The (?!xxx) construct is used for a zero-width negative lookahead. You need the \b word boundary to keep it from matching all but the last letter of a word that's followed by a paren.
EDIT: I have tweaked M42's good idea below so that it works in D. Try this
(\w+)\b([^\(]|$)
The first capture group should contain the words you're interested in.
Outras dicas
How about this one:
/^\(?(\w+)\b(?:[^\(]|$)/
Not tested with D compiler, but it works in Perl.
According to russ comment, the (?:)
construct is unknown in D :
/^\(?(\w+)\b([^\(]|$)/
(\w+)[^\(\w]
so you are negating both the ( and the \w?