felipe
felipe

Reputation: 8025

Regex: Match everything between dots, but exclude entire word if parenthesis is found

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:

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.

Upvotes: 1

Views: 302

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626861

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

Upvotes: 2

Related Questions