带正则表达式的映射列表

2024-10-01 13:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一根线看起来像:

(((ENGL 210) or (COMM 243) or (COMM 205)) and (ECEN 314) and (ECEN 325))

我想把它变成:

^{pr2}$

基本上,将(cccc ddd)形式的字符串中的所有内容映射到cccc ddd,其中c是一个字符,d是一个数字。在

我知道我可以使用re提取所有这样的字符串,但我想将它们映射回新的格式。最干净的方法是什么?在

谢谢。在


Tags: orand字符串re内容格式数字字符
3条回答
import re

t = '(((ENGL 210) or (COMM 243) or (COMM 205)) and (ECEN 314) and (ECEN 325))'
re.sub(r'\(([A-Z]{4} [\d]{3})\)', r'\1', t)

结果

^{pr2}$

解释,re.sub公司第一个论点

r' is going to define a regular expresion inside single quotes

\( is to match the opening parenthesis, this is the one that you want to remove

( opening prenthesis to define a new "group". The things inside this will be stored as a matching "group" as regex group number 1

matching group #1

[A-Z]{4} match four characters uppercase letter

match also a space

[\d]{4} match also four digits

)关闭第1组

\)关闭匹配括号(另一个要删除的)

'关闭regex

解释,re.sub公司第二个论点

r' is going to define a regular expresion inside single quotes

\1 restore the group number one matched in previous argument

'关闭regex

您可以使用^{}来执行此操作:

>>> import re
>>> s = '(((ENGL 210) or (COMM 243) or (COMM 205)) and (ECEN 314) and (ECEN 325))'
>>> re.sub(r'\(([^()]+)\)', r'\1', s)
'((ENGL 210 or COMM 243 or COMM 205) and ECEN 314 and ECEN 325)'

如果你想严格要求格式:

^{pr2}$

以下措施应该有效:

>>> re.sub(r'\((\w{4} \d{3})\)', r'\1', s)
'((ENGL 210 or COMM 243 or COMM 205) and ECEN 314 and ECEN 325)'

re.sub将匹配包含以下内容的模式:

  • 左括号\(
  • 第一组(...)
    • 4个字母\w{4}
    • 空格
    • three digits \d{3}
  • 右括号\)

对于每个匹配,我们将其替换为第一组(\1)的内容,然后得到所需的结果。在

相关问题 更多 >