python-regexHow to match one or more occurence in Python regex?
To match one or more occurence in Python regex, the +
operator can be used. This operator matches one or more occurences of the preceding character or group.
For example,
import re
string = "Hello World"
# Match one or more occurences of 'l'
match = re.search(r'l+', string)
if match:
print(match.group())
Output example
ll
The code above uses the re.search()
function to search for one or more occurences of the character l
in the string Hello World
. The +
operator is used to match one or more occurences of l
. The match.group()
function is then used to print the matched occurences.
Parts of the code:
import re
: imports there
module which provides regular expression matching operationsre.search(r'l+', string)
: searches for one or more occurences of the characterl
in the stringstring
match.group()
: prints the matched occurences
Helpful links
More of Python Regex
- How to perform a zero length match with Python Regex?
- How to get all matches from a regex in Python?
- How to match a YYYY-MM-DD date with Python Regex?
- How to use word boundaries in Python Regex?
- How to match a year with Python Regex?
- How to match whitespace in Python regex?
- How to validate an IP using Python regex?
- How to match a UUID using Python regex?
- How to match a URL path using Python regex?
- How to match a URL using Python regex?
See more codes...