Python Regular Expression – Plus quantifier

Spread the love

Plus quantifier –

The Plus quantifier matches one or more occurrences of the pattern to the left of it.

In [1]: import re

In [2]: re.findall('python+', 'python')
Out[2]: ['python']

In [3]: re.findall('python+', 'pythonnnnn')
Out[3]: ['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 [4]: 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 [5]: re.findall('k[a-z]+', text, flags=re.IGNORECASE)
Out[5]: ['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 [6]: re.findall('\+\d+', '+913678351946 , +918634696281')
Out[6]: ['+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

Rating: 1 out of 5.

Leave a Reply