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 theremodule which provides regular expression matching operationsre.search(r'l+', string): searches for one or more occurences of the characterlin the stringstringmatch.group(): prints the matched occurences
Helpful links
More of Python Regex
- How to match a question mark in Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to remove numbers from a string using Python regex?
- How to get a group from a regex in Python?
- How to match the end of a line with regex in Python?
- How to use word boundaries in Python Regex?
- How to regex match excluding a word in Python?
- How to perform a zero length match with Python Regex?
- How to match whitespace in Python regex?
- How to get all matches from a regex in Python?
See more codes...