
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to match a word in python using Regular Expression?
In Python, Regular Expressions (RegEx) are used to match patterns in the given input strings. The re module in Python provides different methods to use the regular expressions. This module helps the developers to perform search operations, validations, filtering, and much more based on string patterns.
In this article, we will explore different ways to match a word in Python using the re module.
Match a Specific Word using re.search()
In Python, the re.search() method is used to search for a pattern within a given string. If the pattern is found, then it returns a match object otherwise, it returns None. To match a specific word, we can use the pattern with \b, which indicates a word boundary.
Example
In this example, we are using the pattern r'\bcat\b' to match the whole word cat but not like the words catalog or scatter, etc, and the function re.search() checks for the presence of the given input word all over the input string.
import re text = "The cat is on the roof." pattern = r'\bcat\b' # Search for the word 'cat' in the text match = re.search(pattern, text) if match: print("Word found!") else: print("Word not found.")
Following is the output of the above program -
Word found!
Note: The \b represents a word boundary in regular expressions. It ensures that we match the whole word and not part of another word.
Match all Occurrences Using re.findall()
In Python, the re.findall() method is used to find all non-overlapping occurrences of a pattern in a given input string. It returns a list that contains all matches with the given pattern. We can use the defined match pattern with "\b" to match the whole word in the input string.
Example
In this example, we are using the pattern r'\bthe\b' to match all occurrences of the word the in the input string. The re.IGNORECASE flag is used to make the matching case-insensitive so that it matches both "The" and "the".
import re text = "The cat is on the roof with the other cat." pattern = r'\bthe\b' # Find all matches of the word 'the' (case-insensitive) matches = re.findall(pattern, text, re.IGNORECASE) print("Matches found:", matches)
Here is the output of the above program -
Matches found: ['The', 'the', 'the']
Match Words with Case Sensitivity
With the help of the re module, we can perform case-sensitive and case-insensitive matches for the given input text.
By default, regular expression matching is case-sensitive, which means it will only match words that exactly match the case specified in the pattern. To perform case-insensitive matches, we can use re.IGNORECASE flag.
Example
Following is an example that uses the re.IGNORECASE flag in the re.findall() method to match the input string with the defined pattern by ignoring case-sensitive -
import re text = "The cat sat beside another Cat." pattern = r'\bCat\b' # Find all matches of the word 'Cat' (case-sensitive) matches = re.findall(pattern, text,re.IGNORECASE) print("Matches found:", matches)
Following is the output of the above program -
Matches found: ['cat', 'Cat']