- 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
Matching Versus Searching in Python
Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).
Example
#!/usr/bin/python import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj: print "match --> matchObj.group() : ", matchObj.group() else: print "No match!!" searchObj = re.search( r'dogs', line, re.M|re.I) if searchObj: print "search --> searchObj.group() : ", searchObj.group() else: print "Nothing found!!"
Output
When the above code is executed, it produces the following result −
No match!! search --> searchObj.group() : dogs
- Related Articles
- Wildcard Matching in Python
- File Searching using Python
- Regular Expression Matching in Python
- Remove matching tuples in Python
- Unix filename pattern matching in Python
- Template matching using OpenCV in Python
- Pattern matching in Python with Regex
- How do Python Dictionary Searching works?
- Unix filename pattern matching in Python (fnmatch)
- Prefix matching in Python using pytrie module
- Java String.equals() versus ==.
- ADSL versus Cable
- MyISAM versus InnoDB in MySQL?
- calloc() versus malloc() in C
- For Versus While in C++

Advertisements