Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between * and + regex

Tags:

regex

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

[<>]+ [<>]*

like image 375
Few Tem Avatar asked May 26 '12 05:05

Few Tem


People also ask

What is the meaning of * in regex?

The .* is a wildcard expression that matches any sequence of characters including an empty sequence of length=0. grep a.*z matches all of the following strings that start with a and end with z: "abcdefghijklmnopqrstuvwxyz", "abz", "abbz", "ahhhhhz" and "abbdz".

What is difference between wildcard and regular expression?

Wildcards are different from the regular expressions used in grep (although they may look similar at times). Wildcards apply to all commands including grep and are used in place of or in combination with operands. Regular Expressions only apply to grep and a few other UNIX commands.

What does asterisk mean in regex?

The asterisk ( * ): The asterisk is known as a repeater symbol, meaning the preceding character can be found 0 or more times. For example, the regular expression ca*t will match the strings ct, cat, caat, caaat, etc.

What is difference between a B )* and a * b * in regular expression?

So, a+b means [ab] or a|b , thus (a+b)* means any string of length 0 or more, containing any number of a s and b s in any order. Likewise, (a*b*)* also means any string of length 0 or more, containing any number of a s and b s in any order.


5 Answers

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
like image 141
aleroot Avatar answered Oct 17 '22 19:10

aleroot


* 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.

like image 27
Ismail Badawi Avatar answered Oct 17 '22 19:10

Ismail Badawi


+ 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.

like image 7
ghoti Avatar answered Oct 17 '22 17:10

ghoti


* 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.

like image 4
jahroy Avatar answered Oct 17 '22 17:10

jahroy


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)
like image 4
dan radu Avatar answered Oct 17 '22 19:10

dan radu