matching parentheses in python regular expression [duplicate] - python

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

Related

Python efficent non cases sensitive match? [duplicate]

This question already has answers here:
Regex: ignore case sensitivity
(15 answers)
Closed last year.
In python to check if 'apple' appears in another string we do:
if 'apple' in my_str:
To check without cases sensitive I read we can do:
if 'apple' in my_str.lower():
But what if my_str is REALLY long, it's not efficent to call .lower() on it... Doesn't python support native non cases sensitive match?
You can use regular expressions and re.IGNORECASE In python Case insensitive regular expression without re.compile?
import re
s = 'padding aAaA padding'
print(bool(re.findall("aaaa",s,re.IGNORECASE)))
Prints True.

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

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.

Python regex matching on strings I don't want [duplicate]

This question already has answers here:
Python- how do I use re to match a whole string [duplicate]
(4 answers)
Closed 5 years ago.
This is my first attempt at trying to use regex with Python or at all, and it is not working as expected. I want a regex to match any alphabetic character or underscore as the first character, then any number of alphanumeric characters or underscores after. The regex I am using is '^[a-z_,A-Z][a-z_A-Z0-9]*', which seems to produce what I want at pythex.org, but in my code it is matching strings that I do not want.
My code is as follows:
isMatch = re.match('^[a-z_A-Z][a-z_A-Z0-9]*', someString)
return True if isMatch else False
Two examples of strings that are matching that I don't want are: "qq-q" and "va[r". What am I doing wrong?
I think that you just forgot the $ at the end of your regex to specify the end of the string.
isMatch = re.match('^[a-z_A-Z][a-z_A-Z0-9]*$', someString)
Without that, it will match the beginning of the string and not the entire string, which explains why it worked on "qq-q" ("qq" is a match) and "va[r" ("va" is a match).

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

Categories

Resources