我很想得到一些帮助,让正则表达式模式匹配字符串中的多个字符。
潜在输入
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)"]
1条答案
按热度按时间mv1qrgav1#
anubhava提供了答案