// plain
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())
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
onelinerhub: How to match one or more occurence in Python regex?