find a repetitive expression using python regular expression
Hello I am trying to retrieve the f开发者_Go百科ollowing string from a file
neighbors= {5 7 9 11 13 14 15 16 17 }
The pattern {number1 number2... }
varies, some are short some are too long. I want to find such a pattern. My logic is to retrieve the statement "neighbors= {"
which is followed by a number and a space as a repetition till the program finds the last closed braces. Can some one help me out with the syntax?
Thanks
I think you're looking for this:
import re
FOO = """neighbors= {5 7 9 11 13 14 15 16 17 }"""
match = re.search('(neighbors\s*=\s*\{\s*(\d+\s*)+\})', FOO)
print match.group(1)
The regex is portable, of-course to many different languages.
Running that yields...
neighbors= {5 7 9 11 13 14 15 16 17 }
But the regex will match an arbitrary number of digits in curly-braces.
EDIT
Illustrating with re.findall()
and re.compile()
...
import re
FOO = """neighbors= {5 7 9 11 13 14 15 16 17 }"""
COMPILE = re.compile('(neighbors\s*=\s*\{\s*(\d+\s*)+\})')
match = re.findall(COMPILE, FOO)
print match[0]
Running the second code returns...
neighbors= {5 7 9 11 13 14 15 16 17 }
Although you should remember that .findall()
was meant for multiple occurrences of the regex match inside a target string. The examples provided have not illustrated a need for .findall()
I would take the whole line with the word neighbors in it, extract the string that's between the braces, split by space and then I'd have an array of strings which can be converted to integers
this is about what you asked for:
neighbors= \{ (\d+ )+\}
making it more tolerant to some optional spaces around the {} brakets:
neighbors= ?\{ ?(\d+ )+(\}|\d+\})
or a shorter one:
neighbors\s*=\s*\{[\d\s]+\}
精彩评论