
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do we use re.finditer() method in Python regular expression?
According to Python docs,
re.finditer(pattern, string, flags=0)
Return an iterator yielding MatchObject instances over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result.
The following code shows the use of re.finditer() method in Python regex
Example
import re s1 = 'Blue Berries' pattern = 'Blue Berries' for match in re.finditer(pattern, s1): s = match.start() e = match.end() print 'String match "%s" at %d:%d' % (s1[s:e], s, e)
Output
Strings match "Blue Berries" at 0:12
- Related Articles
- Why do we use re.compile() method in Python regular expression?
- How do we use Python Regular Expression named groups?
- Why do we use question mark literal in Python regular expression?
- How do we use Python regular expression to match a date string?
- How do we use a delimiter to split string in Python regular expression?
- Regular Expression "(re)" Sub-Expression in Java
- Regular Expression "re*" Metacharacter in Java
- Regular Expression re+ Metacharacter in Java
- Regular Expression re{ n} Metacharacter in Java
- Sub-Expression "(?: re)" in Java Regular Expressions
- How to use wildcard in Python regular expression?
- How to use range in Python regular expression?
- How to use variables in Python regular expression?
- Regular Expression re{ n, m} Metacharacter in Java
- Explain the Java regular expression construct "re?".

Advertisements