This question already has answers here:
difference between two regular expressions: [abc]+ and ([abc])+
(5 answers)
Closed 5 years ago.
I have the following code:
re.findall(r'(\w)*','2sq')
Why is the result of this programme is: ['q', '']?
I thought it would be ['2','s','q',' '].
Remove the * and you'll get the expected result. * is a greedy, looks like you want each \w and findall will do that.
Related
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
This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
Python regular expression pattern * is not working as expected
(2 answers)
Closed 4 years ago.
import re
real_comp = re.compile(r'[0-9]*')
real_comp.search('+123i').group()
Out[7]: ''
I am expecting the the result as "123", but it returns empty.
What's wrong?
You'll need another quantifier, namely a +:
import re
real_comp = re.compile(r'([0-9]+)')
print(real_comp.search('+123i').group())
Which yields
123
Otherwise the regex engine reports a match before the very first consumed char ( [0-9]* is always true).
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)"
This question already has answers here:
Regular expression - starting and ending with a character string
(3 answers)
Closed 5 years ago.
I am trying to do something like
term3_pattern = re.compile(r'(Sanskar:214) * <SP>')
and then check like that
term3_pattern.match(i):
Can someone help me with the regex pattern?
hey you can try this
import re
re.findall(r'Sanskar:214.*<SP>', s) // s is your string
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.