Python Regex Find Values With Newline Character Also
I am working on this regex problem I'm unable to solve. The regex I've made import re message = '''[key X] value [key X] value value [key X] value value value value [key
Solution 1:
You may use
(?m)^\[([^][]*)]\s+(.*(?:\n(?!\[[^][]*]).*)*)
See the regex demo
Details
^- start of a line\[-[([^][]*)- Group 1: any 0+ chars other than[and]]- a]char\s+- 1+ whitespaces(.*(?:\n(?!\[[^][]*]).*)*)- Group 2:.*- the rest of the line(?:\n(?!\[[^][]*]).*)*- zero or more repetitions of:\n(?!\[[^][]*])- a newline not followed with a[...]substring.*- the rest of the line
import re
message = """[key X] value
[key X] value value
[key X] value
value
value
value
[key ] value
[key ] ?
[key ] ?"""
messageRegex = re.compile(r"^\[([^][]*)]\s+(.*(?:\n(?!\[[^][]*]).*)*)", re.M)
for value in messageRegex.findall(message):
print(value)
Output:
('key X', 'value')
('key X', 'value value')
('key X', 'value\nvalue\nvalue\nvalue')
('key ', 'value')
('key ', '?')
('key ', '?')
Post a Comment for "Python Regex Find Values With Newline Character Also"