1
case1 = http:www.freejobalert.comupsc-advt-no-18337
case2 = http:www.freejobalert.comupsc-advt-no-1833742
job_id = (''.join(re.findall(r'\d{7}:\d{5}',case1,re.I)))

how can i find only 33742 from this above string using regular expression . the number maybe 5 or 7 character.

2
  • 1
    \d{7}|\d{5}... Commented Sep 25, 2018 at 16:50
  • 1
    thanks you..it works Commented Sep 25, 2018 at 16:51

3 Answers 3

3

The issue is that you are not using "or" (|), but instead a colon; Try using:

\d{7}|\d{5}

It should return the number in your string.

Sign up to request clarification or add additional context in comments.

Comments

1

You can also try something like \d{5,7} meaning between 5 and 7 (inclusive) digits.

job_id = (''.join(re.search(r'(\d{5,7})',case1,re.I)))

The default behavior will be greedy, meaning it will match as many digits as possible between 5 and 7 digits.

EDIT (Another option that may be easier to understand but will do the same thing):

job_id = re.search(r'(\d{5,7})',case1).group(1)

Comments

0

Both the before mentioned answers are solutions to your problem, i have sumrises then as follows:

>>> import re
>>> case1 = "http:www.freejobalert.comupsc-advt-no-18337"
>>> case2 = "http:www.freejobalert.comupsc-advt-no-1833742"
>>> job_id1 = (''.join(re.findall(r'\d{5,7}',case1,re.I)))
>>> job_id2 = (''.join(re.findall(r'\d{5,7}',case2,re.I)))
>>> job_id1
'18337'
>>> job_id2
'1833742'

>>> job_id3 = (''.join(re.findall(r'\d{5}|\d{7}',case1,re.I)))
>>> job_id4 = (''.join(re.findall(r'\d{5}|\d{7}',case2,re.I)))
>>> job_id3
'18337'
>>> job_id4
'18337'

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.