How to Grab Number After Word in Python

Find number after a substring in string

Regex: char(\d+)

Details:

  • \d Matches a digit (equal to [0-9])
  • + Matches between one and unlimited times
  • () Capturing group

Python code:

String formatting syntax "%s" % var.The %s token allows to insert (and potentially format) a string.

def find_number(text, c):
return re.findall(r'%s(\d+)' % c, text)

find_number('abce123de34', 'e') >> ['123', '34']
find_number('abce123de34', 'de') >> ['34']

How to extract numbers from a string in Python?

If you only want to extract only positive integers, try the following:

>>> txt = "h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [int(s) for s in txt.split() if s.isdigit()]
[23, 11, 2]

I would argue that this is better than the regex example because you don't need another module and it's more readable because you don't need to parse (and learn) the regex mini-language.

This will not recognize floats, negative integers, or integers in hexadecimal format. If you can't accept these limitations, jmnas's answer below will do the trick.

Extract a number from a string, after a certain character

Assuming:

  • You know there is a comma in the string, so you don't have to search the entire string to find out if there is or not.
  • You know the pattern is 'many_not_digits,few_digits' so there is a big imbalance between the size of the left/right parts either side of the comma.
  • You can get to the end of the string without walking it, which you can in Python because string indexing is constant time

Then you could start from the end and walk backwards looking for the comma, which would be less overall work for your examples than walking from the left looking for the comma.

Doing work in Python code is way slower than using Python engine code written in C, right? So would it really be faster?

  1. Make a string "aaaaa....,12"
  2. use the timeit module to compare each approach - split, or right-walk.
  3. Timeit does a million runs of some code.
  4. Extend the length of "How to Grab Number After Word in PythonHow to Grab Number After Word in Python....,12" to make it extreme.

How do they compare?

  • String split: 1400 "a"'s run a million times took 1 second.
  • String split: 4000 "a"'s run a million times took 2 seconds.
  • Right walk: 1400 "a"'s run a million times took 0.4 seconds.
  • Right walk: 999,999 "a"'s run a million times took ... 0.4 seconds.

!

from timeit import timeit

_split = """num = x.split(',')[-1]"""

_rwalk = """
i=-1
while x[i] != ',':
i-=1
num = x[i+1:]
"""

print(timeit(_split, setup='x="a"*1400 + ",12"'))
print(timeit(_rwalk, setup='x="a"*999999 + ",12"'))

e.g.

1.0063155219977489     # "aaa...,12" for 1400 chars, string split
0.4027107510046335 # "aaa...,12" for 999999 chars, rwalked. Faster.

Try it online at repl.it

I don't think this is algorithmically better than O(n), but with the constraints of the assumptions I made you have more knowledge than str.split() has, and can leverage that to skip walking most of the string and beat it in practise - and the longer the text part, and shorter the digit part, the more you benefit.

Regex to get number after matched pattern

This should work:

info = "'x': 10.61"
items = re.findall("'x':\s([-+]?\d*\.*\d+)", info)
print(items[0]) # 10.61

Regex in Python to get string of numbers after string of letters

Assuming you only care about the numbers at the end of the string, the following expression matches 4 or 5 digits at the end of the string.

\d{4,5}$

Otherwise, the following would be the full regex matching the provided requirements.

^[a-z_]+\d{4,5}$

Extract Number from String in Python

You can filter the string by digits using str.isdigit method,

>>> int(filter(str.isdigit, str1))
3158

For Python3:

int(list(filter(str.isdigit, my_str))[0])



Related Topics



Leave a reply



Submit