Extract Float Values from String in Python (and possibly using Regex)

python extract number from string
python convert string to float
python regex
regex match float or integer
extract numbers from text file python
regex to extract decimal number from string
python regex extract
python regex float or int

I am having problem creating a function that takes a string containing length values (eg: '32.0 mm / 1.259"`) and returning just the value in mm.

My current function parse is only able to handle strings that have just the mm value, but not if both mm and inches value exist.

Any help is greatly appreciated!

Regex pattern: re.sub("[^0-9.\-]", "", str)

import re

def parse(str):
    if not str:
        return None
    str = str.lower()
    return float(re.sub("[^0-9.\-]", "", str))

tests = ['12.3 mm', '12.3mm', '32.0 mm / 1.259"', '32.0mm / 1.259"']
for s in tests: 
    print( parse(s) )

Expected Output


Actual Output

ValueError: could not convert string to float: '32.01.259'

How to extract floating number from text using Python regular , The following code extracts floating numbers from given text/string using Python regex.Exampleimport re s = Sound Level: -11.7 db or 15.2 or 8  Possible Duplicate: How to detect a floating point number using a regular expression. How do I write a Python regular expression that matches string representations of floating point numbers? The expression should match any string that is accepted by the float constructor as in float('3.5'). Thus the expression should match '0.' and '.0' but

Just simplify your regex pattern to the following:

re.sub("mm.*", "", str)

... and you'll the expected output

Python Convert String to float, How do I extract a string from a word in Python? Extract only the portion of a string between two regex patterns Tag: python , regex , string , python-2. This module provides regular expression matching operations similar to those found in Perl. The non-greedy version of the curly brackets, which matches the shortest string possible, has the closing curly bracket followed by a question mark

You could search for the matching pattern instead of using sub, for example:

import re

def parse(s):
    if not s:
        return None
    s = s.lower()
    return float(re.search("(\d+\.\d*\s*)mm", s).group(1))

tests = ['12.3 mm', '12.3mm', '32.0 mm / 1.259"', '32.0mm / 1.259"']

print([parse(test) for test in tests])


[12.3, 12.3, 32.0, 32.0]

How to get the first word in a string in Python, How do you convert a string to a float in Python? Tip: To build and test regular expressions, you can use RegEx tester tools such as regex101. This tool not only helps you in creating regular expressions, but it also helps you learn it. Now you understand the basics of RegEx, let's discuss how to use RegEx in your Python code.

How to extract integers from a string in Python, How do you extract the first word of a string in Python? Python Regex – Get List of all Numbers from String. To get the list of all numbers in a String, use the regular expression ‘[0-9]+’ with re.findall() method.[0-9] represents a regular expression to match a single digit in the string.

Extract decimal numbers from a string in Python, How do you extract an integer from a string in Python? I am trying to create simple crawler with Scrapy (scrapy.org). As per example item.php is allowed. How I can write rule which allow url which always start with http

[Tutor] Python 3.6 Extract Floating Point Data from a Text File, Using RegEx module is the fastest way.>>> import reAssuming that the string contains integer and floating point numbers as well as below  "re" module included with Python primarily used for string searching and manipulation ; Also used frequently for webpage "Scraping" (extract large amount of data from websites) Regular Expression Methods include re.match(),re.search()& re.findall() Python Flags Many Python Regex Methods and Regex functions take an optional argument called Flags

  • Could you please have a look at the answers and choose the one that worked best for you? There are solutions like 1) remving all starting with mm, 2) extracting numbers before mm. My solution is very similar to Daniel's, but it does not extract the number in case of 5. mmorph. since I am using a word boundary and my solution will also work in case of integer numbers before mm.
  • @WiktorStribiżew Thank you