Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between (.+?) and (.*?) in regular expressions

Tags:

regex

I would like to know what the difference is between (.+?) and (.*?)

like image 960
DocWiki Avatar asked Feb 27 '11 06:02

DocWiki


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 * in regex?

The difference is that: the * in this problem can match any sequence independently, while the * in Regex Matching would only match duplicates, if any, of the character prior to it.

What is the difference between * and in regex in Java?

* means any character occurring 0 or more times, a* means a appearing 0 or more times, and so on. Show activity on this post. In regular expressions * by itself does not mean anything. It modifies the expression in front of it.

Why * is used in regex?

- a "dot" indicates any character. * - means "0 or more instances of the preceding regex token"


1 Answers

The .+? form requires at least one character to match, while .*? can match none at all. Both operations are non-greedy, so they will try to find the shortest possible matching string.

like image 195
Jeremiah Willcock Avatar answered Oct 11 '22 10:10

Jeremiah Willcock