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

  1. this figure 3a using ([^.]*?fig.3[^.].)
  2. this fig 4a ([^.]*?fig.4[^.].)
  3. i (figure 5) ([^.]*?fig.5[^.].)
  4. 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,

  • .* before 4 [^.]*
  • 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

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -