Plus quantifier –
The Plus quantifier matches one or more occurrences of the pattern to the left of it.
In : import re In : re.findall('python+', 'python') Out: ['python'] In : re.findall('python+', 'pythonnnnn') Out: ['pythonnnnn']
The plus in ‘python+’ matches one or more occurrences of n in the text as n is to the left of it.
Let’s say that we have some text.
In : text = ''' ...: And I know you'll be ok ...: And if I should die here tonight ...: Darling, know that you saved my life ...: Felt your love every day ...: Not a thing I would change ...: And I know you'll be ok ...: '''
And we want to find all words that starts with k followed one or more letters. We can write.
In : re.findall('k[a-z]+', text, flags=re.IGNORECASE) Out: ['know', 'know', 'know']
Here, the pattern contains a k followed by a character set [a-z] which says that their a single character between a to z and the + quantifier says that there is one or more occurrences of a character between a to z. The re.IGNORECASE make the pattern case-insensitive means it will match both uppercase and lowercase letters.
How to match a Plus character in Regex ?
To match a plus character in a text we have to escape the plus character using a backslash. It removes the special meaning of it in regex.
Let’s say we have some phone numbers in a text and we want to extract all the numbers.
In : re.findall('\+\d+', '+913678351946 , +918634696281') Out: ['+913678351946', '+918634696281']
Here, backslash removes the special meaning from +, then we wrote \d which means any digit character, followed by a + quantifier which says give me one or more occurrences of any digit character.
You can learn more about special characters like \d here – special characters in regex