Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How .* (dot star) works? [closed]

Tags:

regex

I already understand that .* means zero or more of any character, but Could someone explain how .* in the following work and what it would match?

.*([a-m/]*).*  .*([a-m/]+).*  .*?([a-m/]*).* 
like image 321
OpMt Avatar asked Oct 01 '12 02:10

OpMt


People also ask

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

What does dot star mean in regex?

The dot matches any character, and the star allows the dot to be repeated any number of times, including zero. If you test this regex on Put a "string" between double quotes, it matches "string" just fine.

What does dot asterisk mean in Python?

The dot represents an arbitrary character, and the asterisk says that the character before can be repeated an arbitrary number of times (or not at all).

What is Dot in expression?

(dot) operator is used to access class, structure, or union members. The member is specified by a postfix expression, followed by a . (dot) operator, followed by a possibly qualified identifier or a pseudo-destructor name. (A pseudo-destructor is a destructor of a nonclass type.)


2 Answers

the dot means anything can go here and the star means at least 0 times so .* accepts any sequence of characters, including an empty string.

like image 89
Ionut Hulub Avatar answered Sep 27 '22 18:09

Ionut Hulub


Each case is different:

.*([a-m\/]*).*

The first .* will probably match the whole string, because [a-m/] is not required to be present, and the first * is greedy and comes first.

.*([a-m\/]+).*

The first .* will match the whole string up to the last character that matches [a-m/] since only one is required, and the first * is greedy and comes first.

.*?([a-m\/]*).*

The first .*? will match the string up to the FIRST character that matches [a-m/], because *? is not greedy, then [a-m/]* will match all it can, because * is greedy, and then the last .* will match the rest of the string.

like image 43
LMB Avatar answered Sep 27 '22 19:09

LMB