简体   繁体   English

如何在 Python 中使用正则表达式提取格式化的字符串?

[英]How to extract a formatted string using regex in Python?

I am trying to find a regex in Python3 that extract a formatted string from input text : The general format of the string is : 4 uppercase caracters followed by 3 numbers我试图在 Python3 中找到一个从输入文本中提取格式化字符串的正则表达式:字符串的一般格式是:4 个大写字符后跟 3 个数字

Input :输入 :

"JFFC002    /0 EXÉRÈSE LÉS. RÉTROPÉRIT. COELIO 
KFFA001 /0 EXÉRÈSE T. PARAPHARYNGIENNE CERV.TOMIE LACA004   /0 O.S FRAC. ANT. SINUS FRONTAL AB. CORONAL LACA014 /0 O.S FRAC. BILAT. ANT. SINUS FRONTAL AB. CORONAL "

Output :输出 :

['JFFC002' , 'KFFA001' ,'LACA004', 'LACA014' ]

I appreciate your help!我感谢您的帮助!

You can use re.findall with the following pattern:您可以使用具有以下模式的re.findall

s = '''"JFFC002 /0 EXÉRÈSE LÉS. RÉTROPÉRIT. COELIO
KFFA001 /0 EXÉRÈSE T. PARAPHARYNGIENNE CERV.TOMIE LACA004   /0 O.S FRAC. ANT. SINUS FRONTAL AB. CORONAL LACA014 /0 O.S FRAC. BILAT. ANT. SINUS FRONTAL AB. CORONAL "'''

import re

re.findall(r'[A-Z]{4}[0-9]{3}', s)
# ['JFFC002', 'KFFA001', 'LACA004', 'LACA014']

See demo演示

  • Match a single character present in the list below [AZ]{4}匹配下面列表中的单个字符[AZ]{4}
    • {4} Quantifier — Matches exactly 4 times {4}量词 — 精确匹配 4 次
    • AZ a single character in the range between A (index 65) and Z (index 90) (case sensitive) AZ A (索引 65)和Z (索引 90)之间范围内的单个字符(区分大小写)
  • Match a single character present in the list below [0-9]{3}匹配以下列表中的单个字符[0-9]{3}
    • {3} Quantifier — Matches exactly 3 times {3}量词 — 正好匹配 3 次
    • 0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive) 0-9范围在0 (索引 48)和9 (索引 57)之间的单个字符(区分大小写)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM