2024-10-01 13:36:30 发布
网友
我有一根线看起来像:
(((ENGL 210) or (COMM 243) or (COMM 205)) and (ECEN 314) and (ECEN 325))
我想把它变成:
基本上,将(cccc ddd)形式的字符串中的所有内容映射到cccc ddd,其中c是一个字符,d是一个数字。在
(cccc ddd)
cccc ddd
c
d
我知道我可以使用re提取所有这样的字符串,但我想将它们映射回新的格式。最干净的方法是什么?在
re
谢谢。在
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)
结果
解释,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 1matching group #1[A-Z]{4} match four characters uppercase letter match also a space[\d]{4} match also four digits)关闭第1组\)关闭匹配括号(另一个要删除的)'关闭regex
r' is going to define a regular expresion inside single quotes
r'
\( 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 1matching group #1[A-Z]{4} match four characters uppercase letter match also a space[\d]{4} match also four digits)关闭第1组\)关闭匹配括号(另一个要删除的)
\( 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
matching group #1
[A-Z]{4} match four characters uppercase letter
[A-Z]{4}
match also a space
[\d]{4} match also four digits
[\d]{4}
)关闭第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
\1 restore the group number one matched in previous argument
\1
您可以使用^{}来执行此操作:
>>> 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)'
如果你想严格要求格式:
以下措施应该有效:
>>> 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将匹配包含以下内容的模式:
re.sub
(...)
\w{4}
\d{3}
对于每个匹配,我们将其替换为第一组(\1)的内容,然后得到所需的结果。在
结果
^{pr2}$解释,re.sub公司第一个论点
解释,re.sub公司第二个论点
您可以使用^{} 来执行此操作:
如果你想严格要求格式:
^{pr2}$以下措施应该有效:
re.sub
将匹配包含以下内容的模式:\(
(...)
\w{4}
\d{3}
\)
对于每个匹配,我们将其替换为第一组(
\1
)的内容,然后得到所需的结果。在相关问题 更多 >
编程相关推荐