re.match(pattern, string):
- This method finds match if it occurs at start of the string.
- re.match(pattern, string, flags=0)
flags:
- Represent properties or rules to follow while matching the pattern.
- We can specify different flags using bitwise OR (|)
- On success match() function returns match object.
- On failure, it returns None.
- We use group() or groups() function of match object to get matched expression.
import re
line = "python online session"
res = re.match(r'java' , line)
print("Failure : ", res)
res = re.match(r'python' , line)
print("Success : ", res)
Note: ‘r’ is representing ‘raw input’. It is looking into input raw String for specified pattern.
Match object:
- The object which is returned by the function if match found
- Span() : return a tuple containing the start and end positions of match
- .string : returns the string passed into that function
- group() : returns the part of the string where there is a match
import re
line = "python online session"
res = re.match(r'python' , line)
print("Span : ", res.span())
print("String :", res.string)
print("Pattern :", res.group())
output:
Span : (0, 6)
String : python online session
Pattern : python