Hi Everyone,
This is my first time posting a Q in this site...
I have a question regarding regex
The input
string for this regex comes from a xml files which has longitude and latitude values (of all the major cities in the world). I parse those values and would like to split it up based on the
pattern below (using group()). The below regex works fine for most of the values...but doesnt for some other. i.e. I get StackOverflowError.
These values are selected from the GUI which is in the form of a tree. Below is a snippet the
test class I wrote.
The input string maybe be in the forms e.g. 75D15'W or 45D12'20"N
Hence, my question is there any other way that I can write this regex without facing the stack overflow exception. This issue seem to be a know bug in
Java (
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507).
I hope some can help me. Thanking everone in advance....
public class TestPattern {
static final Pattern PATTERN = Pattern.compile(
"(\\d+)D(\\d+)'?(?:(\\d+)\")?([NSEW])");
public static void main (String[] args) {
String val = "75D15'N";
Matcher m = PATTERN.matcher(val);
System.out.println("m.matches() " + m.matches());
System.out.println("m.groupCount() " + m.groupCount());
if(m.matches()){
int deg, min, second;
String dir;
deg=Integer.parseInt(m.group(1));
System.out.println("deg: " + deg);
min=Integer.parseInt(m.group(2));
System.out.println("min: " + min);
if(m.group(3) == null){
second=0;
} else {
second=Integer.parseInt(m.group(3));
}
System.out.println("second: " + second);
dir=m.group(4);
System.out.println("dir: " + dir);
}
}