-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
  • 903
  • 11
  • 23
mrWiga
  • 131
  • 1
  • 2
  • 13
  • 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

3

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
  • 27,144
  • 16
  • 62
  • 82
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
  • 2,903
  • 2
  • 15
  • 21