- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are some basic examples of Python Regular Expressions?
Here are two basic examples of Python regular expressions
The re.match() method finds match if it occurs at start of the string. For example, calling match() on the string ‘TP Tutorials Point TP’ and looking for a pattern ‘TP’ will match. However, if we look for only Tutorials, the pattern will not match. Let’s check the code.
Example
import re result = re.match(r'TP', 'TP Tutorials Point TP') print result
Output
<_sre.SRE_Match object at 0x0000000005478648>
The re.search() method is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. Unlike in re.match() method, here searching for pattern ‘Tutorials’ in the string ‘TP Tutorials Point TP’ will return a match.
Example
import re result = re.search(r'Tutorials', 'TP Tutorials Point TP') print result.group()
Output
Tutorials
Here you can see that, search() method is able to find a pattern from any position of the string but it only returns the first occurrence of the search pattern.
- Related Articles
- Java regular expressions sample examples
- Explain the meanings of some of the regular expressions.
- What are regular expressions in C#
- What are regular expressions in JavaScript?
- What are negated character classes that are used in Python regular expressions?
- What are the properties of Regular expressions in TOC?
- What are anchors in regular expressions in C#?
- What are the regular expressions to finite automata?
- What are the Rules of Regular Expressions in Compiler Design?
- What is Regular Expressions?
- Regular Expression Examples in Python
- What is the groups() method in regular expressions in Python?
- What are the basic concepts of Python?
- Regular Expression in Python with Examples?
- JavaScript Regular Expressions

Advertisements