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.
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:
Python regular expression re.match, why this code does not work? [duplicate]
(1 answer)
Closed 5 years ago.
It appears that python regex is not matching when the target string has a dot. Is this a feature? Am I missing something? Thanks!
>>> import re
>>> re.compile('txt').match('txt')
<_sre.SRE_Match object at 0x7f2c424cd648>
>>> re.compile('txt').match('.txt')
>>>
The docs are quite explicit about the difference between search and match:
Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).
You should therefore replace match with search:
In [9]: re.compile('txt').search('.txt')
Out[9]: <_sre.SRE_Match at 0x7f888afc9cc8>
I don't know the history behind this quirk (which, let's face it, must trip a lot of people up), but I'd be interested to see (in the comments) why re offers a specific function for matching at the start of a string.
In general, I'm broadly against anything whose name is broad enough that you have to go to the docs to work out what it does.
This question already has answers here:
Splitting on last delimiter in Python string?
(3 answers)
Checking whether a string starts with XXXX
(5 answers)
Does Python have a string 'contains' substring method?
(10 answers)
Closed 5 months ago.
I'm trying to compare substrings, and if I find a match, I break out of my loop. Here's an example of a few strings:
'something_tag_05172015.3', 'B_099.z_02112013.1', 'something_tag_05172015.1' ,'BHO98.c_TEXT_TEXT_05172014.88'.
The comparison should only compare the string I'm looking for, and everything in the same strings to what is to the left of the last underscore '_' in the strings. So, 'something_tag' should match only 'something_tag_05172015.3' and 'something_tag_05172015.1'.
What I did to do this was I split on the underscores and did a join on all elements but the last element in the split to compare against my test string (this drops everything to the right of the last underscore. Though it works, there's gotta be a better way. I was thinking maybe regex to remove the last underscore and digits, but it didn't work properly on a few tags.
Here's an example of the regex I was trying: re.sub('_\d+\.\d+', '', string_to_test)
If you are sure that something_tag is in the beggining you can try:
your_tag.startswith('something_tag')
If you are not sure about that:
res = 'something_tag' in your_tag
sobolevn bet me to it. For more complicated scenarios, use a regular expression with named-groups and/or non-capturing groups.
That way the overall string needs to match a specific format, but you can just pull out the sub parts that you're interested in.
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.)
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) ?