re.match() in Python Last Updated : 16 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report re.match method in Python is used to check if a given pattern matches the beginning of a string. It’s like searching for a word or pattern at the start of a sentence. For example, we can use re.match to check if a string starts with a certain word, number, or symbol. We can do pattern matching using re.match in different ways like checking if the string starts with certain characters or more complex patterns. Python import re # Checking if the string starts with "Hello" s = "Hello, World!" match = re.match("Hello", s) if match: print("Pattern found!") else: print("Pattern not found.") OutputPattern found! Table of ContentSyntax of re.matchUsing re.match with Regular ExpressionsAccessing Match ObjectSyntax of re.matchre.match(pattern, string, flags=0)Parameterspattern: This is the regular expression (regex) that you want to match. It can be any string that defines a pattern, like r"\d" for a digit, or r"[A-Za-z]" for any letter (uppercase or lowercase).string: This is the string you want to check for the pattern. re.match will try to match the pattern only at the beginning of the string.flags (optional): This is an optional parameter that allows you to modify how the matching should behave. For example, you can use re.IGNORECASE to make the matching case-insensitive. If not provided, the default value is 0.ReturnsIf re.match finds a match at the beginning of the string, it returns a match object.If no match is found, it returns None.Using re.match with Regular ExpressionsRegular expressions (regex) allow us to search for more complex patterns. For example, let’s check if a string starts with a number. Python import re # Checking if the string starts with a number s = "123abc" # \d means any digit match = re.match(r"\d", s) if match: print("Starts with a number.") else: print("Doesn't start with a number.") OutputStarts with a number. Accessing Match ObjectThe result of re.match is a match object if a match is found, or None if no match is found. If a match occurs, we can get more information about it. Python import re s = "Python is great" match = re.match(r"Python", s) if match: print(f"Match found: {match.group()}") # .group() returns the matched string else: print("No match.") OutputMatch found: Python Comment More infoAdvertise with us Next Article re.match() in Python P pragya22r4 Follow Improve Article Tags : Python python-regex python Practice Tags : pythonpython Similar Reads re.subn() in Python re.subn() method in Python is used to search for a pattern in a string and replace it with a new substring. It not only performs the replacement but also tells us how many times the replacement was made. We can use this method when we need to replace patterns or regular expressions in text and get a 3 min read re.findall() in Python re.findall() method in Python helps us find all pattern occurrences in a string. It's like searching through a sentence to find every word that matches a specific rule. We can do this using regular expressions (regex) to create the pattern and then use re.findall() to get a list of matches.Let's say 2 min read re.finditer() in Python re.finditer() method in Python is used to search for all matches of a pattern in a string and return them as an iterator. In this article, we will explore about re.finditer() method in Python.Let's understand this with the help of an example:Pythonimport re text = "GFG, O, B, GFG, O" pattern = "GFG" 2 min read Python Match Case Statement Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement 7 min read re.search() in Python re.search() method in Python helps to find patterns in strings. It scans through the entire string and returns the first match it finds. This method is part of Python's re-module, which allows us to work with regular expressions (regex) simply. Example:Pythonimport re s = "Hello, welcome to the worl 3 min read Like