Good afternoon, I can not understand using this structure for parsing a string

import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static String fileName = "photo 20101010 101845 89.1380 59.1533 335386058327798 люрмдшнмдшн.jpg"; private static String data; private static double data1; public static void main(String[] arg){ parsingGPS1(); } public static void parsingGPS1(){ try { String variableName = fileName; Pattern pattern = Pattern.compile("(\\s{3}(\\d){1,}.(\\d){1,}\\s{4})"); Matcher matcher = pattern.matcher(variableName); System.out.println("ПРОВЕРКА fileName:" + fileName); System.out.println("ПРОВЕРКА pattern:" + pattern); System.out.println("ПРОВЕРКА matcher:" + matcher); while (matcher.find()) data = matcher.group(); data1 = Double.parseDouble(data.trim()); System.out.println("ПРОВЕРКА:" + data); } catch (Exception e){} } } 

at the output, he always received the first coordinate from line 89.1380, but at some point, the regular expression began to give out 335386058327798 and trying to fix it somehow did not lead to anything. The format of the string has not changed.

Work result

CHECK fileName: photo 20101010 101845 89.1380 59.1533 335386058327798 lu.ms.jpg CHECK pattern: (\ s {3} (\ d) {1,}. (\ D) {1,} \ s {4}) check matcher: java.util .regex.Matcher [pattern = (\ s {3} (\ d) {1,}. (\ d) {1,} \ s {4}) region = 0.84 lastmatch =] CHECK: 335386058327798

Now displayed

CHECK fileName: photo 20161216 102823 89.1380 59.1533 353346056397888 lu.ms.jpg CHECK pattern: (\ s {3} (\ d) {1,}. (\ D) {1,} \ s {4}) check matcher: java.util .regex.Matcher [pattern = (\ s {3} (\ d) {1,}. (\ d) {1,} \ s {4}) region = 0.84 lastmatch =] CHECK: 89.1380
CHECK: 335386058327798

how does the number 335386058327798 fall into a variable, the regular is not written at all?

  • one
    Very simple . (point) in regulars is represented by any character, so you need to screen it so that it is perceived as a point, i.e. replace with \\. and should work. Also , {1,} can be replaced by + - gil9red
  • You are right again. - Varg Sieg

1 answer 1

Probably you should have the following code:

  while (matcher.find()) { data = matcher.group(); data1 = Double.parseDouble(data.trim()); System.out.println("ПРОВЕРКА:" + data); } 
  • Yes, that's right, but I still can’t understand everything. - Varg Sieg