The match method in Python regular expressions
The match
method searches for a match with a regular expression at the beginning of a string. In the first parameter of the method, we specify the regular expression we are going to search for, and in the second parameter, the string in which we are searching for it. If a match is found, the method will return a match object, otherwise - None
. The syntax of the method looks like this:
re.match(what to look for, where to look)
Let's check for example that a string starts with numbers:
txt = '123abc'
res = re.match('\d+', txt)
print(res)
Check that the string starts with the letters:
txt = 'abc 123 bbb 456 987'
Print the match of alphabetic characters at the beginning of a string.
Given a string:
txt = 'aaa bbb 123'
Find the substring containing numbers.