我正在嘗試使用以下正則運算式匹配以字母開頭后跟 2,6 位數字的字串,以下正則運算式匹配 R77 但不匹配 J123,誰能提供有關如何解決此問題的指導?
進口重新
code_free = "[KG6.R77.1.2][J123-P1A-00194]/12C114"
o = re.search(r'(^|[^a-zA-Z0-9:])([a-zA-Z](\d{2,6}[a-zA-Z]?|\d{1}[xX]{1,2}))([^a-zA-Z0-9]|AP|DEV|$)', code_free)
print (o.group(2))
uj5u.com熱心網友回復:
如果我理解正確,只需使用re.findall模式\b[A-Z]\d{2,6}\b:
code_free = "[KG6.R77.1.2][J123-P1A-00194]/12C114"
codes = re.findall(r'\b[A-Z]\d{2,6}\b', code_free)
print(codes) # ['R77', 'J123']
uj5u.com熱心網友回復:
使用re.findall:
(?<![a-zA-Z0-9:])([a-zA-Z](?:\d{2,6}[a-zA-Z]?|\d[xX]{1,2}))(?=[^a-zA-Z0-9]|AP|DEV|$)
請參閱正則運算式證明。
解釋
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
[a-zA-Z0-9:] any character of: 'a' to 'z', 'A' to
'Z', '0' to '9', ':'
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[a-zA-Z] any character of: 'a' to 'z', 'A' to 'Z'
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
\d{2,6} digits (0-9) (between 2 and 6 times
(matching the most amount possible))
--------------------------------------------------------------------------------
[a-zA-Z]? any character of: 'a' to 'z', 'A' to
'Z' (optional (matching the most
amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
[xX]{1,2} any character of: 'x', 'X' (between 1
and 2 times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
[^a-zA-Z0-9] any character except: 'a' to 'z', 'A' to
'Z', '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
AP 'AP'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
DEV 'DEV'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/386006.html
上一篇:在字串中插入反斜杠時遇到問題
