This question already has answers here:
Python- how do I use re to match a whole string [duplicate]
(4 answers)
Closed 6 years ago.
I have two variables storing path in python
var1 = u'Manish/testfolder/.*txt'
var2 = u'Manish/testfolder/test.txt.abc'
if I type
re.match(var1 , var2 )
it yeilds true can someone explain how is it evaluating to true and what I shall I do so that it returns false.
Your regex is included inside var2.
If you explicit set an end for your regex, it's not matching anymore:
var1 = u'Manish/testfolder/.*txt$'
As mentioned here and here re.match() checks for a match only at the beginning of the string.
Related
This question already has answers here:
How to use regex to find all overlapping matches
(5 answers)
Closed 2 years ago.
I have text as below-
my_text = "My telephone number is 408-555-1234"
on which i am searching the pattern
re.findall(r'\d{3}-\d{1,}',my_text)
My intention was to search for three digit numeric value followed by - and then another set of one or more than one digit numeric value. Hence I was expecting the result to be - ['408-555','555-1234'],
However the result i am getting os only ['408-555'] .
Could anyone suggest me what is wrong in my understaning here. And suggest a pattern that would serve my purpose
you can use:
re.findall(r'(?=(\d{3}-\d+))', my_text)
output:
['408-555', '555-1234']
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 4 years ago.
I mean I have this string var:
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
if_logical_string(mystr1) must be True
if_logical_string(mystr2) must be False
How can I achieve this? Is there any lib to do so ?
Thanks.
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
# will output True
print(eval(mystr1))
# will output False
print(eval(mystr2))
If you have a mathematical expression, there is a more elegant way using Pyparsing. Check out this post: from Stackoverflow
Yes, you can use python's eval function.
However, I would recommend having another approach... There's always another solution...
This question already has answers here:
Escaping regex string
(4 answers)
Closed 5 years ago.
Not sure why this is not matching and not working? It appears something is wrong with the regex such that it doesn't match even though i tested it out in the online regex tester
current_name = "bob[0]"
regex_match = re.compile('%s'%current_name)
if re.match(regex_match, current_name):
print "matched"
current_name = "bob[0]"
regex_match = re.compile('%s'%current_name.replace('[', r'\['))
if re.match(regex_match, current_name):
print "matched"
That opening square bracket was causing issues. this will print "matched"
This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 5 years ago.
I want to get userid from url like:
http://space.bilibili.com/1950746/#!/channel/detail?cid=1757&order=&page=1
and it should be like 1950746.
And here is the code:
url='http://space.bilibili.com/1950746/#!/channel/detail?cid=1757&order=&page=1'
b=userid=re.match(r'\d{7}', url)
print(b)
The result is None.
Using re.match gives an implicit ^ at the beginning of the string. Use re.search to find anywhere in string.
This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Python regular expression not matching
(3 answers)
Closed 6 years ago.
In the following code, I expect the program to print "Match", because "\D+\d" matches the "x4" part of the string. But it does not print anything. What is the problem?
import re
pattern = r"\D+\d"
if re.match(pattern, "1x4"):
print("Match");
Thanks
Your assumption that re.match can match anywhere inside a string is wrong.
https://docs.python.org/2/library/re.html#re.RegexObject.match
If zero or more characters at the beginning of string match this regular expression, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.
Use re.search() instead.