26

Can anybody tell me the difference between the * and + operators in the example below:

[<>]+ [<>]*

Ismail Badawi
  • 31,611
  • 6
  • 76
  • 92
Few Tem
  • 565
  • 1
  • 6
  • 16
  • 3
    [What absolutely every Programmer should know about regular expressions](http://wp.me/p2pTzU-4) and [regular-expressions.info](http://www.regular-expressions.info/tutorial.html). Those operators are called "Quantifier" – stema May 26 '12 at 05:52
  • 4
    stema, I wish i could downvote your comment. The point of stackoverflow is to get answers quickly not read more documentation. You might as well have answered lmgtfy. This question comes up as #1 on google when looking for "regex difference between * and +" – Vans S Nov 11 '15 at 16:49

6 Answers6

53

Each of them are quantifiers, the star quantifier(*) means that the preceding expression can match zero or more times it is like {0,} while the plus quantifier(+) indicate that the preceding expression MUST match at least one time or multiple times and it is the same as {1,} .

So to recap :

a*  ---> a{0,}  ---> Match a or aa or aaaaa or an empty string
a+  ---> a{1,}  ---> Match a or aa or aaaa but not a string empty
aleroot
  • 66,082
  • 27
  • 164
  • 205
12

* means zero-or-more, and + means one-or-more. So the difference is that the empty string would match the second expression but not the first.

Ismail Badawi
  • 31,611
  • 6
  • 76
  • 92
  • 4
    Isn't this the other way around? `*` will match on empty, but `+` will not. – Joshua Plicque May 04 '15 at 19:57
  • 1
    I believe Ismail is referring to the OP's sample patterns (`[<>]+` and `[<>]*`) rather than the order of the quantifiers discussed in this answer. I agree, it is a little ambiguous. – mickmackusa Apr 08 '20 at 13:40
7

+ means one or more of the previous atom. ({1,})

* means zero or more. This can match nothing, in addition to the characters specified in your square-bracket expression. ({0,})

Note that + is available in Extended and Perl-Compatible Regular Expressions, and is not available in Basic RE. * is available in all three RE dialects. That dialect you're using depends most likely on the language you're in.

Pretty much, the only things in modern operating systems that still default to BRE are grep and sed (both of which have ERE capability as an option) and non-vim vi.

ghoti
  • 41,419
  • 7
  • 55
  • 93
4

They are quantifiers.

  • + means 1 or many (at least one occurrence for the match to succeed)
  • * means 0 or many (the match succeeds regardless of the presence of the search string)
Darshana
  • 2,366
  • 6
  • 25
  • 49
dan radu
  • 2,692
  • 4
  • 16
  • 23
3

* means zero or more of the previous expression.

In other words, the expression is optional.

You might define an integer like this:

-*[0-9]+

In other words, an optional negative sign followed by one or more digits.

jahroy
  • 20,588
  • 9
  • 52
  • 104
2

[<>]+ is same as [<>][<>]*

Darshana
  • 2,366
  • 6
  • 25
  • 49
detale
  • 10,686
  • 4
  • 35
  • 39