0

I would like to obtain the two numbers on this expression:

s= "sfsdfafafadfafafa **34/23**"

I tried this:

import re 
re.search("\\*\\*[0-9][0-9]\\/[0-9][0-9]\\*\\*",s)

But Im getting None as an ouput, when at least I was expecting to get the expression.

Edit: Please take into consideration the ** as I would like to take them into account

Edit: The numbers can be between 1 and 6 digits

3
  • 1
    Use regex = r"\*\*(\d+)\/(\d+)\*\*" and extract first and second captured group to get the two digits. Commented Apr 6, 2017 at 3:51
  • 1
    Your example regular expression should match fine, but you haven’t showed the code you actually used (re.search takes two arguments, so it can’t return None here). Commented Apr 6, 2017 at 3:51
  • Thanks, I just edited. But even then it gives me none Commented Apr 6, 2017 at 3:54

2 Answers 2

2

Use this regex instead:

(\*\*\d+\/\d+\*\*)

\d is the same as [0-9], it denotes all digits.

Regex101:

https://regex101.com/r/bHhrKj/3

Apply to your code (You omitted the second argument):

import re 
re.search("(\*\*\d+\/\d+\*\*)", inputString)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, and that regex link is very useful
0

Doing:

re.findall(r'\*\*(\d{2})\/(\d{2})\*\*', 'sfsdfafafadfafafa **34/23**')

will return an array containing a tuple [('34', '23')] that you can use to get the values from the string.

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.