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

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.

Related

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

find all string that starts with "Sanskar:214" and end with "<SP>" using re in python [duplicate]

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

Cofused with regex in Python [duplicate]

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.

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

Remove "x" number of characters from a string? [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Understanding slicing
(38 answers)
Closed 8 years ago.
In Python: How do I write a function that would remove "x" number of characters from the beginning of a string?
For instance if my string was "gorilla" and I want to be able remove two letters it would then return "rilla".
OR if my string was "table" and I wanted to remove the first three letters it would return "le".
Please help and thank you everyone!
You can use this syntax called slices
s = 'gorilla'
s[2:]
will return
'rilla'
see also Explain Python's slice notation

Categories

Resources