Skip to content Skip to sidebar Skip to footer

Regular Expression Get Number From String

First I am using python 2.7 I have these possibilities of the string: 3 paths 12 paths 12 path rooms Question what is the reqular expression to get the number without the text.

Solution 1:

You say you can only use scrapy methods, so I guess you're after:

hxs.select('//some/xpath/expression/text()').re(r'(\d+).*')

Solution 2:

you can use this: Regex = [\d]*

Solution 3:

An alternative way would be to use [0-9] instead of \d

import re

def extract_number(string):
    r = re.compile('[0-9]+')
    return r.match(string).group()

Solution 4:

(\d+).*\n for pulling the numbers and then skipping the rest of the line.

number_finder = re.compile('(\d+).*\n') number_finder.findall(mystr)

will output an array of the number values

Example:

In [3]: r = re.compile('(\d+).*\n') In [4]: r.findall('12 a \n 12 a \n') Out[4]: ['12', '12']

Solution 5:

The regex pattern to look for is \d. So in python you would code it as:

pattern = re.compile(r'\d+')
result =  re.search(pattern, input_string)

Post a Comment for "Regular Expression Get Number From String"