[英]How to Match mutilple line string using Python Regex?
I have a below 2 lines : 我有以下2行:
/begin MEASUREMENT ANYNAME1 "Unterstützungskraft Softwaremodul " /开始测量ANYNAME1 “ UnterstützungskraftSoftwaremodul ”
SWORD ANYNAME2 1 100 - Randomdigits1 Randomdigits2 剑ANYNAME2 1100 - Randomdigits1 Randomdigits2
and I want to match ANYNAME1 , ANYNAME2 , Randomdigits1 and Randomdigits2 并且我想匹配ANYNAME1 , ANYNAME2 , Randomdigits1和Randomdigits2
So far I am able to match ANYNAME1 in first line using below regex : 到目前为止,我可以使用以下正则表达式在第一行中匹配ANYNAME1 :
_regex_struct = re.compile(r'/begin MEASUREMENT (.*)(.*)\n')
but i am not able to go to the second line. 但是我不能去第二行。 How to match the expression on second line?? 如何匹配第二行上的表达式?
I just make an assumption with your input. 我只是假设您的输入。 You may check the RegexDemo . 您可以检查RegexDemo 。
inputstr = '''/begin MEASUREMENT ANYNAME1 "Unterstützungskraft Softwaremodul "
SWORD ANYNAME2 1 100 -2342342523 2432343535654
'''
_regex_struct = re.compile(r'/begin\s+MEASUREMENT\s+(?P<name1>[\w.]+)\W.*\nSWORD\s+(?P<name2>[\w.]+)\W.+\s+(?P<digit1>-\d.+|\d.+)\s+(?P<digit2>-\d.+|\d.+)')
_regex_struct.findall(inputstr)
Output: 输出:
[('ANYNAME1', 'ANYNAME2', '-2342342523', '2432343535654')]
Explanation of the expression: 表达式说明:
\\s
= any whitespace character\\s
=任何空格字符
(?P<>)
= to create a group of the expected output(?P<>)
=创建一组预期的输出
\\w
= any word character\\w
=任何单词字符
\\W
= any non-word character\\W
=任何非单词字符
\\d
= any digit\\d
=任何数字
+
= to express one or more+
=表示一个或多个
In [20]: s = '''/begin MEASUREMENT ANYNAME1 "Unterstützungskraft Softwaremodul "
...: SWORD ANYNAME2 1 100 -Randomdigits1 Randomdigits2'''
In [31]: re_struct = re.compile(r'/begin MEASUREMENT (\w+)[\s\S]*?SWORD (\w+).*?100 -(\w+) (\w+)')
In [32]: m = re_struct.search(s)
In [33]: m.group(1), m.group(2), m.group(3), m.group(4)
Out[33]: ('ANYNAME1', 'ANYNAME2', 'Randomdigits1', 'Randomdigits2')
You could match ANYNAME1 in a capturing group in the first line, then use .*
to get to the end of the line and use \\n
to match a new line to get to the second line. 您可以在第一行的捕获组中匹配ANYNAME1,然后使用.*
到达该行的末尾,并使用\\n
匹配一个新行以到达第二行。 There you could match and capture your values using 3 groups. 在那里,您可以使用3个组来匹配并捕获您的值。
/begin MEASUREMENT ([\w.]+).*\nSWORD ([\w.]+) \d+ \d+ (-?\d+(?:\.\d+)?) (-?\d+(?:\.\d+)?)
Regex demo | 正则表达式演示 | Python demo Python演示
Explanation 说明
/begin MEASUREMENT
Match literally followed by a space /begin MEASUREMENT
逐字匹配后跟一个空格 ([\\w.]+).*\\n
Capture 1+ word chars or a dot in group 1 and match until the end of the string. ([\\w.]+).*\\n
捕获第1组中的1个以上的字符字符或点,并匹配至字符串末尾。 Then match a newline 然后换行 SWORD ([\\w.]+)
Match SWORD and capture in group 2 1+ times a word char or dot SWORD ([\\w.]+)
匹配SWORD并在组2中捕获1+次单词char或点 \\d+ \\d+
Match space, 1+ digits, space, 1+ digits space \\d+ \\d+
匹配空格,1 +位数字,空格,1 +位数字 (-?\\d+(?:\\.\\d+)?) (-?\\d+(?:\\.\\d+)?)
Capture in group 3 and 4 an optional minus sign, 1+ digits and an optional decimal part whith a space in between (-?\\d+(?:\\.\\d+)?) (-?\\d+(?:\\.\\d+)?)
在第3和第4组中捕获可选的负号,1 +数字和可选的小数部分两者之间的空间 For example: 例如:
import re
regex = r"/begin MEASUREMENT ([\w.]+).*\nSWORD ([\w.]+) \d+ \d+ (-?\d+(?:\.\d+)?) (-?\d+(?:\.\d+)?)"
test_str = ("/begin MEASUREMENT ANY.NAME1 \"Unterstützungskraft Softwaremodul \"\n"
"SWORD ANYN.AME2 1 100 -2342342523 -14.29")
print(re.findall(regex, test_str))
# [('ANY.NAME1', 'ANYN.AME2', '-2342342523', '-14.29')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.