This question already has answers here:
How to parse for tags with '+' in python
(2 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 3 years ago.
I would like to get the regular expression of this kind of expressions
title="+34 952387749
title="+34 123456789
But I've got a problem with the space and +. As far I got this piece of code but I don't find how to express the space and +
^title="+34' space [0-9]{9}
Many thanks for your help !
Escape the special characters like space and + using .
In RegEx, space is denoted by "\s".
RegEx for your expression would be:
\+34\s\d{9}
\d is used to denote digit.
You need backslashed to specify that you specifically want to search for the +. Your regex would be:
\+34 [0-9]{9}
Try use this regex:
^title="\+\d{2} \d{9}$
Related
This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 3 years ago.
I'm not familiar with regular expressions and am not sure what I'm doing wrong.
reg=re.compile('[a-zA-z]+?') #regular expression checks for at least one alphabetic character
print(bool(reg.match('*ab*')))
I would like this to result in True. It doesn't matter where the alphabetic character occurs in the string.
You can also change your pattern if you want to keep compile and match:
re.compile('.*[A-Za-z].*')
You can check your matches by using the re.match function.
Here is a doc on it: https://www.guru99.com/python-regular-expressions-complete-tutorial.html
import re
string = "someWord"
Output = re.match('[a-zA-z]+?', string)
if Output:
print('match found')
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I am a beginner in regular expressions in python, and I was hoping to understand the following line of code:
HTML_TAG_REGEX = re.compile(r'<[^>]*>', re.IGNORECASE)
I know that re.compile creates a regular expression object, and that the 'r' tells python we're dealing with a regular expression; however, I was hoping someone could explain what's going on with the rest of the code and specifically the usage of the less than/greater than signs. Thank you!
Your expression:
matches a "<" character
Then matches 0 or more characters that are not ">"
matches a ">" the end of the pattern
As pointed above, the r before the string means raw string, not regular expression.
You can use a regex translator to get these details.
This question already has answers here:
What is the need for caret (^) and dollar symbol ($) in regular expression?
(5 answers)
Closed 4 years ago.
My doubt is that I came across a regex which checks whether a password is strong or not. What is the impact of ^ and $ in this expression.
a = compile(r'^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{8,}$')
It has ^ and $ signs in it. But the below code works the same as above.
a = compile(r'(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{8,}')
If so why are they been used in the above code. Or is there reason for its usage. Thanks in advance!
The ^ means “beginning of a line” and the $ means “end of a line”.
In your case, every match is a line so you don't have any problem.
^ is followed by the string or pattern by which the string will be started with and $ follows the string or pattern by which the string will be ended with. For your case your regex matched with the pattern of the string without regarding the starting or ending portion.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
Hi I am trying to understand python code which has this regular expression re.compile(r'[ :]'). I tried quite a few strings and couldnt find one. Can someone please give example where a text matches this pattern.
The expression simply matches a single space or a single : (or rather, a string containing either). That’s it. […] is a character class.
The [] matches any of the characters in the brackets. So [ :] will match one character that is either a space or a colon.
So these strings would have a match:
"Hello World"
"Field 1:"
etc...
These would not
"This_string_has_no_spaces_or_colons"
"100100101"
Edit:
For more info on regular expressions: https://docs.python.org/2/library/re.html
This question already has answers here:
Do regular expressions from the re module support word boundaries (\b)?
(5 answers)
Closed 5 years ago.
It is known that \b means word boundary in regular expression. However the following code of re module in python doesn't work:
>>> p=re.compile('\baaa\b')
>>> p.findall("aaa vvv")
[]
I think the returned results of findall should be ["aaa"], however it didn't find anything. What's the matter?
You need to use a raw string, or else the \b is interpreted as a string escape. Use r'\baaa\b'. (Alternatively, you can write '\\b', but that is much more awkward for longer regexes.)