Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ".*" and ".+" give different results?

Tags:

regex

Why does ".*" and ".+" give different results?

System.out.println("foo".replaceAll(".+", "bar")); // --> "bar"
System.out.println("foo".replaceAll(".*", "bar")); //--> "barbar"

I would expect "bar" for both, since * and + are both greedy and should match the whole String. (The above example is Java, but other Tools, like http://www.gskinner.com/RegExr/ give me the same result)

like image 313
Tim Büthe Avatar asked Nov 12 '09 10:11

Tim Büthe


1 Answers

You're right about both being greedy but ".*" is matching two strings: the first one is "foo" and the second is "". ".+" will only match "foo".

Both try to match the longest possible string which is "foo". After that, they try to find the longest matching string coming after the previous match. In this phase, ".*" is able to match an empty string while ".+" won't.

like image 66
mmx Avatar answered Sep 20 '22 16:09

mmx