1

I have a string which have following pattern. String will always remain same. Just numbers will be different

Showing Results (1 – 15 of 96,831)

I want to extract 96,831 from that string. I want to do this with regex. What can be regex for that? I have tried a way where I am using two regex but still not getting required number.

"Showing Results (1 – 15 of 96,831)".replace(/[a-zA-Z\(\)]+/g, '').replace(/(\d+ – \d+)/g, '')

Its output is as follow. This output contains spaces which are not required.

"    96,831"

I want a way to do this in python. Can anyone help me?

4
  • 4
    You do not seem to need any regex here. Split and trim the last entry from the ). Is it really Python? Your code is JS. Commented Aug 1, 2016 at 11:05
  • Also, why I want to use this regex in python if it still not getting required number? Commented Aug 1, 2016 at 11:07
  • @WiktorStribiżew Sorry for unclear statement. I mean I want a way in python to do this as if someone want to give example, he can give in python. Commented Aug 1, 2016 at 11:09
  • 1
    You clearly don't need a regex here (Assuming all the required strings will be of same format). The answer by @Iron Fist can be used. Commented Aug 1, 2016 at 11:14

4 Answers 4

3

This is clear case where you should avoid regex as it just needs simple strip and split, like so:

>>> s = 'Showing Results (1 – 15 of 96,831)'
>>> num = s.split()[-1]
'96,831)'
>>> num.strip(')')
'96,831'

Or, using str.rstrip:

>>> num = s.rsplit(maxsplit=1)[-1]
>>> num
'96,831)'
>>> num.strip(')')
'96,831'

But if you insist on using regex, then this pattern might do the job for you:

>>> import re
>>> nums = re.findall(r'\d+,?\d*', s)
['1 ', '15 ', '96,831']
>>> nums[-1]
'96,831'

And if you want to convert it to float don't forget to replace , with .:

>>> num
'96,831'
>>> 
>>> num = num.replace(',','.')
>>> num
'96.831'
>>> float(num)
96.831
Sign up to request clarification or add additional context in comments.

5 Comments

If the string has hello 6180 9 hello it returns '6180 9' as a result (which should not be the case right?). So the regex you have used is kind of hard coded for the string.
@KartheekPalepu...This is a very general case and OP has to add more details to his requirement, I'm just providing a general purpose solution for him...that's all.
Plus your example is different than OP's one, which is way I said...OP has to provide more details.
Okay Great. I was just curious how a global regex looks like which suites for all input strings.
Thanx for detailed answer. :)
3

One compact way of doing it without regex:

str = "Showing Results (1 – 15 of 96,831)"
print str.split(" ")[-1].strip(")")

Comments

2

NOTE: I assume regex is necessary.

import re

print (re.findall(re.compile(u'of ([0-9,]+)'), u"Showing Results (1 – 15 of 96,831)")[0].replace(",", ""))

Comments

2

Assuming all of your test strings share the same format you can simply do:

>>> import re
>>> regex = "\d+,\d+"
>>> pattern = re.compile(regex)
>>> str = "Showing Results (1 – 15 of 96,831")
>>> pattern.findall(str)
['96,831']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.