我有以下字符串:
"these 13 keys are 4, C1, C2, 11, 12, 26, 54, 70, 75, 80, 87, 89 and 95 some other 2 text"
我正在尝试使用正则表达式提取密钥。为了使密钥有效,有几种条件:
are
and
因此结果应为:
4
C1
C2
11
12
26
54
70
75
80
87
89
95
数字,13
并且2
不应该作为关键结果返回。
我使用过,(?= )[^,]+
但是第一个和最后一个结果不正确(获取13 keys are 4
和and 95 some other text
)作为结果。
这是一种替代方法:
(\w+(?=,)|(?<=and\s)\w+)
说明:
( # start of matching group
\w+ # a key
(?=,) # followed by a comma - without taking it
| # or
(?<=and\s) # preceded by "and " - without taking it
\w+ # a key
) # end of matching group
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句