Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the regex (.*?) and (.*)?

Tags:

regex

I've been doing regex for a while but I'm not an expert on the subtleties of what particular rules do, I've always done (.*?) for matching, but with restriction, as in I understood it would stop the first chance it got, whereas (.*)? would continue and be more greedy.

but I have no real reason why I think that, I just think it because I read it once upon a time.

now I'd like to know, is there a difference? and if so, what is it...

like image 391
Christopher Thomas Avatar asked Dec 04 '12 14:12

Christopher Thomas


People also ask

What does .*?) Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.

What is the difference between .*? And * regular expressions?

represents any single character (usually excluding the newline character), while * is a quantifier meaning zero or more of the preceding regex atom (character or group). ? is a quantifier meaning zero or one instances of the preceding atom, or (in regex variants that support it) a modifier that sets the quantifier ...

What is the difference between A * and A+ regular expression?

Note that a* means zero or more occurrence of a in the string while a+ means that one or more occurrence of a in the string. That means a* denotes language L = {є , a, aa, aaa, ….}

Are there different types of regex?

There are also two types of regular expressions: the "Basic" regular expression, and the "extended" regular expression.


1 Answers

(.*?) is a group containing a non-greedy match.

(.*)? is an optional group containing a greedy match.

like image 120
SLaks Avatar answered Oct 21 '22 01:10

SLaks