I have a string variable in java having value:
Code: Select all
String result="34.1 -118.33\n<!--ABCDEFG-->";
Code: Select all
String result="34.1 -118.33";
How can I do this? Your help would be highly appreciated, Thanks in advance!
String result="34.1 -118.33\n<!--ABCDEFG-->";
String result="34.1 -118.33";
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\s*-\\s*\\d{1,3}\\.\\d{1,3}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("34.1 -118.33\n<!--ABCDEFG-->");
boolean matchFound = matcher.find();
if(matchFound) {
System.out.println(matcher.group(0));
} else {
System.out.println("Match not found");
}
}
}