Home > Software engineering >  Regex: Match everything between dots, but exclude entire word if parenthesis is found
Regex: Match everything between dots, but exclude entire word if parenthesis is found

Time:05-03

I'm currently trying to find a regex pattern that allows me to match dot separated words past config., but not match any words that have open parenthesis. Example:

  • config.hello.world should match hello.world
  • config.hello should match hello
  • config.hello.world( should match hello
  • config.hello.world*10 should match hello.world

I've been unable to figure out the parenthesis part. The first, second, and fourth example above I've gotten to work via

\bconfig\.([\w\.] )

Any help would be appreciated, thank you.

CodePudding user response:

You can use

\bconfig\.([\w.] )\b(?![(\w])

See the regex demo. Details:

  • \b - a word boundary
  • config\. - a config. substring
  • ([\w.] ) - Group 1: one or more word or . chars
  • \b - a word boundary
  • (?![(\w]) - a negative lookahead that fails the match if there is a word or ( char immediately to the right of the current location.

See the Python demo:

import re
texts = ['config.hello.world','config.hello','config.hello','config.hello.world']
rx = re.compile(r'\bconfig\.([\w.] )(?![(\w])')
for text in texts:
    m = rx.search(text)
    if m:
        print(text   " => "   m.group(1))
    else:
        print(text   " => NO MATCH")

Output:

config.hello.world => hello.world
config.hello => hello
config.hello => hello
config.hello.world => hello.world
  • Related