簡體   English   中英

Python 字符串匹配的正則表達式

[英]Python Regular Expression for String Matching

我可以使用哪個正則表達式來匹配模式字符串 - XX-YYY-XXXXXZ,其中 X 是任何數字,YYY 是需要匹配的任何字母模式,Z 是字母表。

例如。 如果三個字符串是 89-ABC-98765Z、76-GHI-67453H、76-ABC-76453A 我需要帶有“ABC”的 output 字符串,即 89-ABC-98765Z、76-ABC-76453A

嘗試使用 str.match(r'.[0-9][ABC][0-9][AZ]?')。 PS 我正在嘗試在 dataframe 列中使用它。

import re

pattern = r'\d+-[A-Z]+-\d+[A-Z]'
text = 'for eg. if three strings are 89-ABC-98765Z, 76-GHI-67453H, 76-ABC-76453A I need the output strings with "ABC" i.e 89-ABC-98765Z, 76-ABC-76453A'
res = re.findall(pattern,text)
print(res)

正則表達式應該是:

\d{2}-ABC-\d{5}[A-Z]

解釋:

\d{2}   # 2 digits
-ABC-   # literal "-ABC-"
\d{5}   # 5 digits
[A-Z]   # any uppercase letter

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM