我要在此字符串中匹配“ windowsXP”:
windowsXP|7|8
或者
windowsXP 7 8
我写了一个模式来匹配它:
([^0-9|])+[0-9\.]+.*
为什么不匹配?是| 麻烦在支架上。我尝试将其转义,但还是没有用。
([^0-9\|])+[0-9\.]+.*
看:
([^0-9|])+
查找与数字和管道不同的字符[0-9\.]+
尝试匹配数字或句点,但是表达式无法捕获到管道。使用
^([^|\s]+)[|\s](\d+)[|\s](\d+)
见证明
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^|\s]+ any character except: '|', whitespace (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
[|\s] any character of: '|', whitespace (\n, \r,
\t, \f, and " ")
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
[|\s] any character of: '|', whitespace (\n, \r,
\t, \f, and " ")
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \3
import re
regex = r"^([^|\s]+)[|\s](\d+)[|\s](\d+)"
test_str = "windowsXP|7|8"
print(re.findall(regex, test_str))
结果:[('windowsXP', '7', '8')]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句