Regex for all kind of number variations [duplicate] - python

This question already has answers here:
How to extract a floating number from a string [duplicate]
(7 answers)
How to extract numbers from a string in Python?
(19 answers)
Closed 6 years ago.
I am using python to parse some strings that contain numbers and I want to find a regex that will extract all kind of scenarios:
.2345 0.934 12.3 11.0
Tried something like:
((\-|\+)?[0-9]+(\.[0-9]+)?)
But it seems like cases with .number are not covered.

Your RegEx is correct, but you want to parse numbers which starts with . also, so you can add \. along with \-|\+ as follow:
((\-|\+)?(\.)?[0-9]+(\.[0-9]+)?)
Note: It will match .1.1

Related

String pattern in Python [duplicate]

This question already has answers here:
How do I validate a date string format in python?
(5 answers)
Closed 6 months ago.
Im trying to check if a user's input is following the pattern integer/integer/integer(like month/day/year) but i dont know how to use exactly the match function to define that the pattern contains "number",then "/",again "number" and "/"...
Check out https://regex101.com/ for a neat website to check your regex! This is implemented in python using the re library. https://docs.python.org/3/library/re.html
In your case, the pattern would be [0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}

Validating Regex US ZIP CODE with constraints in Python [duplicate]

This question already has answers here:
regex for zip-code
(3 answers)
Closed 2 years ago.
I'm trying to write a regex that follows these constraints:
Exactly 5 digits
Sometimes, but not always, followed by a dash with 4 more digits
Zip code needs to be preceded by at least one whitespace
Cannot be at the start of a text
I've arrived at this but it's not giving me the output I want:
r"^[A-Za-z].*\s.*\d{5}(?:[-\s]\d{4})?$"
I would use:
(?<=[ \t])((?:\d{5}(?=[^\d-]|$))|(?:\d{5}-\d{4}(?=[^\d-]|$)))
Demo and explanation

Numeric pattern search in regular expression using Python [duplicate]

This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
Closed 2 years ago.
I have text as below-
my_text = "My telephone number is 408-555-1234"
on which i am searching the pattern
re.findall(r'\d{3}-\d{1,}',my_text)
My intention was to search for three digit numeric value followed by - and then another set of one or more than one digit numeric value. Hence I was expecting the result to be - ['408-555','555-1234'],
However the result i am getting os only ['408-555'] .
Could anyone suggest me what is wrong in my understaning here. And suggest a pattern that would serve my purpose
you can use:
re.findall(r'(?=(\d{3}-\d+))', my_text)
output:
['408-555', '555-1234']

Use Python 3 str.format to truncate a float to have no more than x number of digits [duplicate]

This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 4 years ago.
I am looking for an expression that will truncate a float to at most a certain number of digits. I want to preserve a certain number of decimals, without having unnecessary trailing 0s.
So, this almost works as desired:
"{0:3.f"}.format(number)
For input 3.123000001:
"{0:.3f}".format(3.1230000001)
'3.123'
Great. But for input 3:
"{0:.3f}".format(3)
'3.000'
I figured out the answer while I was writing the question. Just add .rstrip('0') to the expression. So:
"{0:3.f}".format(number).rstrip('0')

python split regex into multiple lines [duplicate]

This question already has answers here:
How to split long regular expression rules to multiple lines in Python
(6 answers)
Closed 4 years ago.
Just a simple question. Lets say i have a very long regex.
regex = "(foo|foo|foo|foo|bar|bar|bar)"
Now i want to split this regex into multiple lines. I tried
regex = "(foo|foo|foo|foo|\
bar|bar|bar)"
but this doesnt seems to work. I get different outputs. Any ideas?
Just do it like this
regex = "(foo|foo|foo|foo" \
"|bar|bar|bar)"

Categories

Resources