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.
Related
This question already has answers here:
Regex plus vs star difference? [duplicate]
(9 answers)
Python regular expression pattern * is not working as expected
(2 answers)
Closed 4 years ago.
import re
real_comp = re.compile(r'[0-9]*')
real_comp.search('+123i').group()
Out[7]: ''
I am expecting the the result as "123", but it returns empty.
What's wrong?
You'll need another quantifier, namely a +:
import re
real_comp = re.compile(r'([0-9]+)')
print(real_comp.search('+123i').group())
Which yields
123
Otherwise the regex engine reports a match before the very first consumed char ( [0-9]* is always true).
This question already has answers here:
Regular expression - starting and ending with a character string
(3 answers)
Closed 5 years ago.
I am trying to do something like
term3_pattern = re.compile(r'(Sanskar:214) * <SP>')
and then check like that
term3_pattern.match(i):
Can someone help me with the regex pattern?
hey you can try this
import re
re.findall(r'Sanskar:214.*<SP>', s) // s is your string
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:
Error in regex to catch special characters
(2 answers)
Closed 6 years ago.
Can someone explain the following result?
Input to python 2.7.12 shell
re.match('[!##$%^&*()-_=+\|\[{\]};:\'<,.>/?~`]+', '2222').group()
Output:
'2222'
I don't understand why digits match this expression.
re.match('[!##$%^&*()-_=+\|\[{\]};:\'<,.>/?~`]+', '2222').group()
# ^^^
)-_ inside the brackets is a character range, and 2 is in that range.
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.