python - Pattern for '.' separated words with arbitrary number of whitespaces -


it's first time i'm using regular expressions in python , can't work.

here want achieve: want find strings, there word followed dot followed word. after unknown number of whitespaces followed either (off) or (on). example:

word1.word2   (off) 

here have come far.

string_group = re.search(r'\w+\.\w+\s+[(\(on\))(\(off\))]', analyzed_string) 

\w+ first word

\. dot

\w+ second word

\s+ whitespaces

[(\(on\))(\(off\))] (off) or (on)

i think last expression might not doing need to. implementation right now, program find right place in string, output of

string_group.group(0) 

is just

word1.word2   ( 

instead of whole expression i'm looking for. please give me hint doing wrong?

[ ... ] used character class, , match one character inside them unless put quantifier: [ ... ]+ 1 or more time.

but adding won't work...

\w+\.\w+\s+[(\(on\))(\(off\))]+ 

will match garbage stuff word1.word2 )(fno(nofn too, don't want use character class, because it'll match characters in order. can use capturing group, , non-capturing group along or operator |:

\w+\.\w+\s+(\((?:on|off)\)) 

(?:on|off) match either on or off

now, if don't parentheses, caught in first group, can change to:

\w+\.\w+\s+\((on|off)\) 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -