0

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.

2 Answers 2

2

You have a couple of problems here:

  1. You're escaping the . after 1357, which means a literal ., which isn't what you meant to have
  2. You aren't treating the * characters (which do need to be escaped, of course).

To make a long story short:

re.findall(r'1357\*(.*?)\*2468', a)
Sign up to request clarification or add additional context in comments.

Comments

1

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.