• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular Expressions (Pattern/Matcher)

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

}

}
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey the code that you gave how do you input the Second Input 45D12'20"N.

Please provide some more input which might cause the failure.
As
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to parse the degrees, minutes, seconds,

75D15'W or 45D12'20"N

I'm not sure what that leading "(?:" does in ".....(? : ( \\d+)\")..."

what if you used the regular expression


where the degrees are now group 2, minutes are group 4, seconds group 6, and direction group 7.
- i don't know enough about these geocodes to know if you can actually have 20"N (no degrees, no minutes). this regular expression allow for any or all of them to be present.

and a test class for this regular expression.

 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The test code you attached works perfectly fine.

Hi if the pattern is not working you make a simple java code also for this it will work.
45D12'20\"N",
"45DN",
"45D12'W",
"18\"S",

you can make a generic method for the same problem.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic