I want to know if there is a difference in these two regular expressions:
Pattern.compile("\"title\":\"(.*?)\"");
Pattern.compile("\"title\":\".*\"");
The part (.*?)
and .*
looks like they have the same meaning...
Here I get exactly the same results:
String title = null;
Pattern p = Pattern.compile("\"title\":\"(.*?)\"");
//Pattern p = Pattern.compile("\"title\":\".*\"");
Matcher m = p.matcher("sdfssdfsdfsdfsdf\"title\":\"Here is the title\"sdfgdfgdfgdfgdfg");
if (m.find()) {
title = m.group();
}
System.out.println(title);
Output:
"title":"Here is the title"
If I do not use parentheses - I'm still able to find separate groups like that:
Pattern p = Pattern.compile("\"title\":\".*?\"");
Matcher m = p.matcher("sdfssdfsdfsdfsdf\"title\":\"Here is the title\"dfdfgrt\"title\":\"Here is the title\"");
while (m.find()) {
System.out.println(m.group());
}
The output:
"title":"Here is the title"
"title":"Here is the title"
So - do I really need parentheses here?