I have a string: a = '*1357*0123456789012345678901234567890123456789*2468*'
I want to find a value between 1357
and 2468
which is 0123456789012345678901234567890123456789
.
I want to use regex or easier method to extract the value.
I tried re.findall(r'1357\\.(.*?)2468', a)
, but I don't know where I'm doing wrong.
You have a couple of problems here:
.
after 1357
, which means a literal .
, which isn't what you meant to have *
characters (which do need to be escaped, of course). To make a long story short:
re.findall(r'1357\*(.*?)\*2468', a)
If you want a slightly more general or flexible method, you can use this:
re.findall(r'\*\d+\*(\d+)\*\d+\*',a)
Which gives you the same output:
['0123456789012345678901234567890123456789']
But the advantage is that it gives you the value between any set of numeric values that are surrounded by the *
. For instance, this would work for your string, but also for the string a = *0101*0123456789012345678901234567890123456789*0*
, etc...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.