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

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

Related

Python REGEX to exclude beggining of string [duplicate]

This question already has answers here:
Match text between two strings with regular expression
(3 answers)
Closed 3 years ago.
Given the following string:
dpkg.log.looker.test.2019-09-25
I'd want to be able to extract:
looker.test
or
looker.
I have been trying multiple combinations but none that actually extract only the hostname. If I try to filter the whole beggining of the file (dpkg.log.), it also ignores the subsequent characters:
/[^dpkg.log].+(?=.[0-9]{4}-[0-9]{2}-[0-9]{2})/
returns:
er.test
Is there a way to ignore the whole string "dpkg.log" without ignoring the subsequent repeated characters?
Maybe, the following expression would be working just OK with re.findall:
[^.]+\.[^.]+\.(.+)\.\d{2,4}-\d{2}-\d{2}
Demo
Test
import re
regex = r'[^.]+\.[^.]+\.(.+)\.\d{2,4}-\d{2}-\d{2}'
string = '''
dpkg.log.looker.test.2019-09-25
dpkg.log.looker.test1.test2.2019-09-25
'''
print(re.findall(regex, string))
Output
['looker.test', 'looker.test1.test2']

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

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

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

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