import re text text see Chapter see Chapter regex Chapter print text p

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import re
text = "text (see Chapter 3.4.5.1) (see Chapter 8.7)";
regex = r'Chapter (\d)(\.\d)?(\.\d)?(\.\d)?'
print text
print re.findall(regex, text, re.S)
regex = r'Chapter (\d)(\.\d)+?'
print re.findall(regex, text, re.S)
regex = r'Chapter (\d)(\.\d)+'
print re.findall(regex, text, re.S)
text (see Chapter 3.4.5.1) (see Chapter 8.7)
[('3', '.4', '.5', '.1'), ('8', '.7', '', '')]
[('3', '.4'), ('8', '.7')]
[('3', '.1'), ('8', '.7')]