why \b doesn't work in python re module? [duplicate] - python

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

Related

regular expression that matches on at least one alphabetic character [duplicate]

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

Regular Expression Python, space and plus [duplicate]

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}$

Understanding Regex Expressions in Python [duplicate]

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.

understanding this python regular expression re.compile(r'[ :]') [duplicate]

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

matching parentheses in python regular expression [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 1 year ago.
I have something like
store(s)
ending line like "1 store(s)".
I want to match it using Python regular expression.
I tried something like re.match('store\(s\)$', text)
but it's not working.
This is the code I tried:
import re
s = '1 store(s)'
if re.match('store\(s\)$', s):
print('match')
In more or less direct reply to your comment
Try this
import re
s = '1 stores(s)'
if re.match('store\(s\)$',s):
print('match')
The solution is to use re.search instead of re.match as the latter tries to match the whole string with the regexp while the former just tries to find a substring inside of the string that does match the expression.
Python offers two different primitive
operations based on regular
expressions: match checks for a match
only at the beginning of the string,
while search checks for a match
anywhere in the string (this is what
Perl does by default)
Straight from the docs, but it does come up alot.
have you considered re.match('(.*)store\(s\)$',text) ?

Categories

Resources