0

I have some numbers within sentences and they look like this

s='rare blond Canadian terrier on wheels. Only $8.98. test test. ...0.1/10 very rare'

patt='[0-9\.{1}]*/10'
re.findall(patt,s)

i want to find float/10 ( or sometimes int/10) , but with my pattern i got

['...1/10']

im sure this should be an easy fix, anyone please help?

ikel
  • 1,790
  • 6
  • 31
  • 61

1 Answers1

1

You can use:

\d+(?:\.\d+)?/10
  • \d+ matches one or more digits

  • The optional (?) non-captured group (?:\.\d+) matches a . followed by one or more digits

  • /10 matches literal /10

Example:

In [72]: str_ = 'rare blond Canadian terrier on wheels. Only $8.98. test test. ...0.1/10 very rare'

In [73]: re.search(r'\d+(?:\.\d+)?/10', str_).group()
Out[73]: '0.1/10'

In [74]: str_ = 'rare blond Canadian terrier on wheels. Only $8.98. test test. ...23/10 very rare'

In [75]: re.search(r'\d+(?:\.\d+)?/10', str_).group()
Out[75]: '23/10'
heemayl
  • 39,294
  • 7
  • 70
  • 76