-1

New to python and need help with regex please.

How do I extract the number 10000000000 from the string below

/s-seller/John/10000000000/time/1

Please note the word John is dynamic and number 10000000000 can also be any random numbers.

Thank you

leiyc
  • 881
  • 6
  • 19
mrWiga
  • 85
  • 1
  • 1
  • 8
  • That string contains two separate numbers. How do you decide which one do you want? – John Gordon Aug 15 '18 at 01:27
  • Possible duplicate of [Reference - What does this regex mean?](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – pault Aug 15 '18 at 01:30

2 Answers2

2
import re

line = "/s-seller/John/10000000000/time/1"

m = re.search(r'/(\d+)/', line)

print(m.group(1)) # 10000000000

the regex expression r'/(\d+)/' you can use.

pwxcoo
  • 1,951
  • 1
  • 9
  • 20
1

If you want to get the first number:

import re
regex = re.compile('.*\/(\d+)\/.*')
regex.match(your_str).group(1)

the (\d+) is a capture group that will match your number.


A simpler approach without regexes would be to split the string by /:

[int(d) for d in your_str.split('/') if d.isdigit()]
jh314
  • 24,533
  • 14
  • 58
  • 79