regex Python正则表达式:可能匹配括号内的多个字符

7vux5j2d  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(177)

我很想得到一些帮助,让正则表达式模式匹配字符串中的多个字符。
潜在输入

  • The paragraph E(1)
  • The paragraph E(1)(a)
  • The paragraph E(1)(a)(ii)
  • The paragraph E(1)(a)(ii) and paragraph G(1)(b)

产出

  • E(1)
  • E(1)(a)
  • E(1)(a)(ii)
  • E(1)(a)(ii)G(1)(b)

到目前为止,我有下面的正则表达式

import re

# Wrong. Should be ["E(1)"]
re.findall("[A-Z]\(\d{1,3}\)\([a-z]\)","The paragraph E(1)")
>>> []

# Correct
re.findall("[A-Z]\(\d{1,3}\)\([a-z]\)","The paragraph E(1)(a)")
>>> ["E(1)(a)"]

# Wrong. Should be ["E(1)(a)(ii)"]
re.findall("[A-Z]\(\d{1,3}\)\([a-z]\)","The paragraph E(1)(a)(ii)")
>>> ["E(1)(a)"]

# Wrong. Should be ["E(1)(a)(ii)", "G(1)(b)"]
re.findall("[A-Z]\(\d{1,3}\)\([a-z]\)","The paragraph E(1)(a)(ii) and paragraph G(1)(b)")
>>> ["E(1)(a)", "G(1)(b)"]
mv1qrgav

mv1qrgav1#

anubhava提供了答案

re.findall("[A-Z]\(\d{1,3}\)(?:\([a-z]+\))*","The paragraph E(1)")
>>> ["E(1)"]

re.findall("[A-Z]\(\d{1,3}\)(?:\([a-z]+\))*","The paragraph E(1)(a)")
>>> ["E(1)(a)"]

re.findall("[A-Z]\(\d{1,3}\)(?:\([a-z]+\))*","The paragraph E(1)(a)(ii)")
>>> ["E(1)(a)(ii)"]

re.findall("[A-Z]\(\d{1,3}\)(?:\([a-z]+\))*","The paragraph E(1)(a)(ii) and paragraph G(1)(b)")
>>> ["E(1)(a)(ii)", "G(1)(b)"]

相关问题