python - Match second number that follows a given pattern -
i have strings of kind
hello 45 blabla 12 100 code 45 13 093bb
i'd match second number follows keyword code
(with python). in example "13"
should returned.
here solution found
s = "hello 45 blabla 12 100 code 45 13 093bb " re.search("code \d+ \d+",s).group(0).split(" ")[-1] # '13'
it works there better solutions. have tried using lookbehind run issue python doesn't tolerate lookbehind of underfined length (and don't know number of digits in first number follows code
). tried that
re.search("(?<=code \d+)\d+",s).group(0) # raise error, v # invalid expression # sre_constants.error: look-behind requires fixed-width pattern
you skip re
together
>>> = s.split() >>> a[a.index('code') + 2] '13'
Comments
Post a Comment