Ethan Chung
Ethan Chung

Reputation: 45

Regex to Find a number after a string

I'm given this value ?({mvp:375760,ms:6}) and I want to be able to only get the number 6 after ms: not the numbers before. I'm fairly new to regex and I don't know how to exclude the ms. I am able to get ms:6 but not just the value 6 by itself.

I tried ms.[0-9] but that gets 'ms:6'

Upvotes: 2

Views: 1479

Answers (1)

Mark
Mark

Reputation: 92440

You can use a positive lookbehind (?<=) to find a value that follows something else. Here you want a the digit(s) that follow the string ms:. That would look like (?<=ms:)\d+:

import re

s = '?({mvp:375760,ms:6})'

re.search(r'(?<=ms:)\d+', s).group()
# '6'

or if there could be more than one:

re.findall(r'(?<=ms:)\d', s)
# ['6']

Upvotes: 2

Related Questions