[英]Regex to match one of multiple strings
Need help with regex to match either of the following:需要正则表达式方面的帮助以匹配以下任一项:
data.testID=abd.123,
data.newID=abc.123.123,
data.testcaseID=abc.1_2,
data.testid=abc.123,
data.TestCaseID=abc.1.2,
I have tried with我试过
m = re.search("data.[test.+|new]?[ID]?=(.+)?[,\}]")
You can use您可以使用
m = re.search(r"data\.(?:test\w*|new)?(?:ID)?=([^,]+)", text, re.I)
See the regex demo .请参阅正则表达式演示。 Details :
详情:
data\\.
- data.
data.
string (note the escaped .
).
)(?:test\\w*|new)?
- an optional test
+ zero or more word chars or new
strings test
+ 零个或多个字字符或new
字符串(?:ID)?
- an optional ID
substring ID
子串=
- a =
sign =
- a =
符号([^,]+)
- Group 1: one or more chars other than ,
. ([^,]+)
- 第 1 组:除,
之外的一个或多个字符。 See a Python demo :看一个Python 演示:
import re
texts = ['data.testID=abd.123,','data.newID=abc.123.123,','data.testcaseID=abc.1_2,','data.testid=abc.123,','data.TestCaseID=abc.1.2,']
rx = re.compile(r'data\.(?:test\w*|new)?(?:ID)?=([^,]+)', re.I)
for text in texts:
m = rx.search(text)
if m:
print(text, '=>', m.group(1))
Output:输出:
data.testID=abd.123, => abd.123
data.newID=abc.123.123, => abc.123.123
data.testcaseID=abc.1_2, => abc.1_2
data.testid=abc.123, => abc.123
data.TestCaseID=abc.1.2, => abc.1.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.