Search code examples
regexdregex-lookaroundsparentheses

Regex for matching a word without a ( after it


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.


Solution

  • 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.