繁体   English   中英

如何使用Python Regex匹配以下字符串?

[英]How to match the following strings using Python Regex?

使用Python Regex,我很想知道

  1. 如何匹配模式("Exercises...) ("Chapter...)("Chapter...)
  2. 如何替换("Exercises...) ("Exercises...)) ("Chapter...)("Chapter...)("Chapter...

例如:

("Exercises, 31" "#42")被转换为("Exercises, 31" "#42"))

("Chapter 2 I Positive Borel Measures, 33" "#44") ("Chapter 2 I Positive Borel Measures, 33" "#44"

谢谢并恭祝安康!

>>> import re
>>> re.sub(r'(\("Exercises.*?\))', r'\1)', '("Exercises, 31" "#42")')
'("Exercises, 31" "#42"))'
>>> re.sub(r'(\("Chapter.*?)\)', r'\1', '("Chapter 2 I Positive Borel Measures, 33" "#44")')
'("Chapter 2 I Positive Borel Measures, 33" "#44"'

暂无
暂无

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

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