Regex plus vs stellar difference?

What's the difference between:

(.+?) 

and

 (.*?) 

when i use it in my php preg_match regex?

+47
php regex
Dec 20 '11 at 12:14
source share
9 answers

They are called quantifiers.

* 0 or more from the previous expression

+ 1 or more from the previous expression

By default, the quantifier is greedy, which means that it matches the maximum possible number of characters.

? after the quantifier changes its behavior to make this quantifier “jagged”, means that it will match as little as possible.

Greedy / jagged example

For example, in the string "abab"

a.*b will match "abab" (preg_match_all will return one match, "abab")

while a.*?b will only match the start "ab" (preg_match_all will return two matches, "ab")

You can test your regular expressions online, for example. on regexr, see the greedy example here

+81
Dec 20 '11 at 12:18
source share

The first ( + ) is one or more characters. The second ( * ) is zero or more characters. Both are not greedy ( ? ) And match anything ( . ).

+15
Dec 20 '11 at 12:17
source share

+ is minimal, * may also be null.

+5
Dec 20 '11 at 12:16
source share

A + matches one or more instances of the previous template. A * matches zero or more instances of the previous pattern.

Basically, if you use + , there must be at least one instance of the template, if you use * , it will still match if there are no instances of it.

+4
Dec 20 '11 at 12:17
source share

+ matches at least one character

* matches any number (including 0) characters

? denotes a lazy expression, so it will match as few characters as possible.

+4
Dec 20 '11 at 12:17
source share

A star is very similar to a plus, the only difference is that when a plus corresponds to 1 or more preceding characters / groups, the beginning corresponds to 0 or more.

+3
Dec 20 '11 at 12:18
source share

Consider below the string that will match.

 ab 

Sample (ab.*) Will return a match for the capture group with the result ab

Until the pattern (ab.+) Matches and returns nothing.

But if you change the line to the next, it will return aba for the pattern (ab.+)

 aba 
+2
Jan 09 '16 at 7:16
source share

I think that in the previous answers it was not possible to single out a simple example:

for example, we have an array:

 numbers = [5, 15] 

The following regex ^[0-9]+ expression matches only 15 . However, ^[0-9]* matches as 5 and 15 . The difference is that the + operator requires at least one duplicate of the previous regex expression

0
Mar 07 '17 at 15:15
source share

In RegEx, {i,f} means "between i and f matches." Let's look at the following examples:

  • {3,7} means 3 to 7 matches
  • {,10} means up to 10 matches without a lower limit (that is, the lower limit is 0)
  • {3,} means at least 3 matches without an upper limit (that is, the upper limit is infinite)
  • {,} means that the upper limit or lower limit for the number of matches (that is, the lower limit is 0 and the upper limit is infinite)
  • {5} means exactly 4

Most good languages ​​have abbreviations, so RegEx:

  • + is short for {1,}
  • * is short for {,}
  • ? is an abbreviation for {,1}

This means that + requires at least 1 match, while * accepts any number of matches or no matches at all, ? accepts no more than one match or zero.

Credit: Codecademy.com

0
Dec 08 '17 at 4:19 on
source share



All Articles