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 matchhello.world
config.hello
should matchhello
config.hello.world(
should matchhello
config.hello.world*10
should matchhello.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 boundaryconfig\.
- aconfig.
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