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).
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:
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:
How to use regex to find all overlapping matches
(5 answers)
How to find overlapping matches with a regexp?
(4 answers)
Closed 5 years ago.
I'm using Python 3's re.findall to find all occurrences of a substring within a string:
import re
full_string = 'ABCDCDC'
sub_pattern = 'CDC'
re.findall(sub_pattern, full_string)
re.findall only finds ['CDC'], however, the pattern CDC occurs 2 times in the full string.
Why isn't re.findall finding all occurrences of CDC here?
What's needed so that re correctly finds all occurrences?
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.