-2

I saw this regular expression being used in a program - (.+?) But I don't understand What does this mean. I know that, . is for any character except newline + is for one or more characters ? is for zero or one character

But don't understand what this entire regex (.+?) convey.

3 Answers3

2

The parenthesis mean a capturing group. The .+ would match any character 1 or more times. The ? makes it work in a non-greedy fashion.

Study Regular Expression How To - it covers all of the parts of this regular expression.

This expression alone does not make much sense, and is usually a part of an expression, sample:

>>> import re
>>> s = "Hello, World!"
>>> re.match(r"(.+?), World!", s).group(1)
'Hello'
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0
.+?

is any character . besides for new line potentially multiple times, but as fewest times as possible.

For example, say you has a regular expression that matches (.+?)a and you pass in

aba

then ab, or multiple characters in this case, will be in the capturing group in order to be able to match the a at the end after at least one character.

Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
-1

As you said . is for any character so .+ will be a series of characters matching 0 or more times and adding a ? will make it work whether the pattern exists or does not exists.

Rohan Amrute
  • 764
  • 1
  • 9
  • 23