In this tutorial, you will learn to search substring patterns using python with re.match method. The re.match()
the function will search the regular expression pattern and return the first occurrence.
We can search for patterns of text using the regular expression module ‘re’ in python.
The match function is used to match the Regular Expression(RE) pattern to a string with optional flags. If the search is successful, match returns a corresponding match object instance. If the search fails, match returns None.
To match multiple strings, we use the |
character as a logical OR
operator. The match()
method is similar to the search()
method, but it doesn’t accept a regular expression as an argument.
Python re match Example
The match()
takes two arguments- a pattern and a string. If they match, it returns the string otherwise returns None. The re.match()
will only match at the beginning of the string and not at the beginning of each line.
The Syntax re.match
The match method syntax is as follows:
re.match(pattern, string, flags=0)
Parameters:
pattern
: The regular expression.string
: The string to be checked for a match.flags
: Some regular expression options, such as:re.I
: For a case insensitive search.re.L
: Causes words to be interpreted according to the current locale.re.S
: Performs a period (dot) match at any character, including a new line.re.U
: Interprets letters according to the Unicode character set. How \w, \W, \b, and \B behave are usually affected.
Checkout other python String tutorials:
- How To Convert Python String To Array
- How to Trim Python string
- Python String join Example
- Python Array of Strings
- How To Match String Item into List Python
- How To Convert String to int and int to string
- How To Use Regex With Python
Simple Use of re.match() method
We need to import re package at the top of the file and then access match method as follows:
import re pattern = '^py…n$' source_str = 'Hello, I am pythonpip blog admin' result = re.match(pattern, source_str) if result: print("Substring is found in string '{0}' " .format(source_str)) else: print("Substring is not found in string '{0}' " .format(source_str))
In the above code, We have used re.match()
a function to search pattern within the source_str
. The method returns a match object if the search is successful. If not, it returns None.
Python Regex – Metacharacters
A Regular Expression (RegEx) is a sequence of characters that defines a search pattern.
Python has the following metacharacters:
Metacharacter | Description |
^ | Matches the start of the string |
. | Matches a single character, except a newline But when used inside square brackets, a dot is matched |
[ ] | A bracket expression matches a single character from the ones inside it [abc] matches ‘a’, ‘b’, and ‘c’ [a-z] matches characters from ‘a’ to ‘z’ [a-cx-z] matches ‘a’, ’b’, ’c’, ’x’, ’y’, and ‘z’ |
[^ ] | Matches a single character from those except the ones mentioned in the brackets[^abc] matches all characters except ‘a’, ‘b’ and ‘c’ |
( ) | Parentheses define a marked subexpression, also called a block, or a capturing group |
\t, \n, \r, \f | Tab, newline, return, form feed |
* | Matches the preceding character zero or more times ab*c matches ‘ac’, ‘abc’, ‘abbc’, and so on [ab]* matches ‘’, ‘a’, ‘b’, ‘ab’, ‘ba’, ‘aba’, and so on (ab)* matches ‘’, ‘ab’, ‘abab’, ‘ababab’, and so on |
{m,n} | Matches the preceding character minimum m times, and maximum n times a{2,4} matches ‘aa’, ‘aaa’, and ‘aaaa’ |
{m} | Matches the preceding character exactly m times |
? | Matches the preceding character zero or one times ab?c matches ‘ac’ or ‘abc’ |
+ | Matches the preceding character one or one times ab+c matches ‘abc’, ‘abbc’, ‘abbbc’, and so on, but not ‘ac’ |
| | The choice operator matches either the expression before it, or the one after abc|def matches ‘abc’ or ‘def’ |
\w | Matches a word character (a-zA-Z0-9) \W matches single non-word characters |
\b | Matches the boundary between word and non-word characters |
\s | Matches a single whitespace character \S matches a single non-whitespace character |
\d | Matches a single decimal digit character (0-9) |
\ | A single backslash inhibits a character’s specialness Examples- \. \\ \* When unsure if a character has a special meaning, put a \ before it: \@ |
$ | A dollar matches the end of the string |
informative, I love to read this post.