I know $ is used to check if a line end follows in a Java regular expression.
For the following codes:
String test_domain = "http://www.google.com/path\nline2\nline3";
test_domain = test_domain.replaceFirst("(\\.[^:/]+).*$?", "$1");
System.out.println(test_domain);
The output is:
http://www.google.com
line2
line3
I assume that the pattern (\\.[^:/]+).*$? matches the first line, which is http://www.google.com/path, and the $1 is http://www.google.com. The ? makes a reluctant match (so matches the first line.)
However, if I remove the ? in the pattern and implement following codes:
String test_domain = "http://www.google.com/path\nline2\nline3";
test_domain = test_domain.replaceFirst("(\\.[^:/]+).*$", "$1");
System.out.println(test_domain);
The output is:
http://www.google.com/path
line2
line3
I think it should give out the result http://www.google.com
(\\.[^:/]+)matcheshttp://www.google.com.*$matches/path\nline2\nline3
Where is my misunderstanding of the regex here?