简体   繁体   中英

How to match the following strings using Python Regex?

Using Python Regex, I was wondering

  1. how to match the patterns ("Exercises...) and ("Chapter...)
  2. how to replace ("Exercises...) with ("Exercises...)) , and ("Chapter...) and ("Chapter... .

For example:

("Exercises, 31" "#42") is converted to ("Exercises, 31" "#42"))

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

Thanks and regards!

>>> 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"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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