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"
Related
This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 3 years ago.
import re
pattern = re.compile(r"/")
a = "a/b"
I tried
re.sub(pattern, '\/', a)
#(also, a.replace('/', '\/'))
#output
a\\/b
What I want is
a\/b
a.replace('/', '\\/')
the first \ is an escape character, so you need to type it twice to have the real \.
You can use if it's not compulsory to use regex:
a = "a/b"
a=a.replace("/","\/")
print(a)
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 exactly is a "raw string regex" and how can you use it?
(7 answers)
Closed 6 years ago.
I am trying to remove tags in text that are identified by a backslash. For example, for the phrase 'Hello \tag world', I'd like to return the phrase 'Hello world'. I've tried the following but it doesn't get rid of the '\tag'.
print re.sub('\\[A-Za-z]+',' ',text)
I'm sure it's something simple, but I can't seem to figure it out.
Thanks for any help you can give!
Must be:
re.sub('\\\\[A-Za-z]+',' ',text)
Otherwise, '\\' is treated as a regex special escape character.
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.
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 7 years ago.
I'm trying to extract all attribute values from a large xml file
s = '<some id="Foo" menu="BAAR"></some>'
output = re.findall( '="(.*)"' ,s)
print output
I'm expecting out put
['Foo','BAAR']
However I'm getting
['Foo" menu="BAAR']
Can anyone please help me pointing out what I'm doing wrong ?
In regular expression * is greedy, that means, it takes as much as it can. Just use the non-greedy version *?:
s = '<some id="Foo" menu="BAAR"></some>'
output = re.findall( '="(.*?)"' ,s)
print output