Python regex expression to match pattern in sentences -
given paragraph example
.this figure 3a. fig 4a . (figure 5). important (fig 6a).
i python regex extract sentences based on figure number. trying
- this figure 3a using ([^.]*?fig.3[^.].)
- this fig 4a ([^.]*?fig.4[^.].)
- i (figure 5) ([^.]*?fig.5[^.].)
- this important (fig 6a) ([^.]*?fig.6[^.].)
but matching not specific. number 4 example extract figures. 1 specific figures based on figure number
you need replace,
.*
before4
[^.]*
- replace
4
\d
code:
in[3]: s = "this figure 3a. fig 4a . (figure 5). important (fig 6a)." in[4]: import re in[5]: re.findall(r'[^.]*?fig[^.]*\d[^.]*', s) out[5]: ['this figure 3a', ' fig 4a ', ' (figure 5)', ' important (fig 6a)']
or
in[8]: re.findall(r'\s*([^.]*?fig[^.]*\d[^.]*?)(?=\s*\.)', s) out[8]: ['this figure 3a', 'this fig 4a', 'i (figure 5)', 'this important (fig 6a)']
Comments
Post a Comment