Massoud Afrashteh Massoud Afrashteh - 1 year ago 67
Java Question

REGEX mistake in java

I have two line that are as the same but they have two different results!

public static String data =
"Rome:Jan 81.2,Feb 63.2,Mar 70.3,Apr 55.7,May 53.0,Jun 36.4,Jul 17.5,Aug 27.5,Sep 60.9,Oct 117.7,Nov 111.0,Dec 97.9" +
"\n" +
"London:Jan 48.0,Feb 38.9,Mar 39.9,Apr 42.2,May 47.3,Jun 52.1,Jul 59.5,Aug 57.2,Sep 55.4,Oct 62.0,Nov 59.0,Dec 52.9" +

public static void main(String[] args) {
System.out.println(mean("London", data));

public static double mean(String town, String strng) {
Matcher matched=checkExists(town + "(.*?)\n", data);
if (matched!=null)
return calAvr(",");
return -1;

private static Matcher checkExists(String regex, String source) {
Matcher matcher = Pattern.compile(regex).matcher(source);
if (matcher.find()) return matcher;
return null;

private static double calAvr(String splitData) {
double res = 0.0;
int count = 0;
//Matcher matcher = Pattern.compile("\\s(.*?),").matcher(splitData);
Matcher matcher=checkExists("\\s(.*?),", splitData);
while (matcher.find()) {
res += Double.parseDouble(;
return res / count;

These two line are as the same:

//Matcher matcher = Pattern.compile("\\s(.*?),").matcher(splitData);
Matcher matcher=checkExists("\\s(.*?),", splitData);

When I use the first one it find all the numbers.In second one I just return the matcher object and use it but it could not find "48.0" which is the first occurrence! I have used the matcher object in mean func and that works fine!

Answer Source

When you call Matcher matcher=checkExists("\\s(.*?),", splitData); you are executing find() for the first time, and then you use while (matcher.find()) which means than you just have lost the first match cause you are executing find after the first find without dealing with data.

This does not happen, however, when you use Matcher matcher = Pattern.compile("\\s(.*?),").matcher(splitData); instead...

see documentation here

public boolean find() Attempts to find the next subsequence of the input sequence that matches the pattern. This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download