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

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

Related

How to use regex pattern to match longer string? [duplicate]

This question already has answers here:
How to use regex with optional characters in python?
(5 answers)
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have a text string, similar to below example,
I have 5-6 year of experience with 2-3 years experience in Java
I have used this below regex syntax to match it,
import re
pattern = '\d{1}-\d{1} year'
[(m.start(0), m.end(0),'Experience') for m in re.finditer(pattern, string)]
# results
5-6 year
2-3 year (In this case it's missing out the 's'.)
How to modify this pattern to also match 'years and year' which every is longest?
Add an optional "s": '\d{1,2}-\d{1,2}\s*years?'. I also changed '\d{1}' to '\d{1,2}' which means "one or two digits" (it's hard to imagine someone has more than 99 years of experience), and replaced one space with '\s*' - any number of spaces, including no spaces.

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']

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)"

Regex for all kind of number variations [duplicate]

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

Why does regex [!##$%^&*()-_=+\|\[{\]};:\'<,.>/?~`]+ match digits? [duplicate]

This question already has answers here:
Error in regex to catch special characters
(2 answers)
Closed 6 years ago.
Can someone explain the following result?
Input to python 2.7.12 shell
re.match('[!##$%^&*()-_=+\|\[{\]};:\'<,.>/?~`]+', '2222').group()
Output:
'2222'
I don't understand why digits match this expression.
re.match('[!##$%^&*()-_=+\|\[{\]};:\'<,.>/?~`]+', '2222').group()
# ^^^
)-_ inside the brackets is a character range, and 2 is in that range.

Categories

Resources