0

Referring to this question here. I have a new Problem. I want to extract the last two appearing numbers (longitude/latitude) from a String.

I use the approach from the referenced answer. A simple example where the error occures can be found here:

public static void main(String[] args) {
    String input = "St. Louis (38.6389, -90.342)"; 

    Pattern p = Pattern.compile("-?[\\d\\.]+");
    Matcher m = p.matcher(input);

    while(m.find()){
        System.out.println(m.group());
    }
}

Console output is as following:

.
38.6389
-90.342

The Problem appears to be the "." in "St. Louis". Can someone help me to solve this issue in a nice way?

Thanks a lot for every answer/comment.

3 Answers 3

3

Change your regex like below,

Pattern p = Pattern.compile("-?\\d+(?:\\.\\d+)?");

(?:\\.\\d+)? at the last would make the decimal part as optional.

Sign up to request clarification or add additional context in comments.

2 Comments

This fails to catch .9, which is also a common way to write 0.9, Including in java's syntax
then this "-?\\d*(?:\\.\\d+)?", but i think .9 would be written as 0.9
3
[-+]?\\d+(\\.\\d+)?

Use this for floating numbers and integers.

The Problem appears to be the "." in "St. Louis"

Thats because of -?[\\d\\.]+ [] the character class.It can match any character defined inside class.

5 Comments

This fails to catch .9, which is also a common way to write 0.9, Including in java's syntax
@amit i guess that should be clarified with OP if he wants it or not
Why wouldn't he? It's a legitimate writing, maybe the OP should also explicitly clarify if he wants to include the digit 5 as well?
@amit because these are longitudes and latitudes.i m not very sure there these numbers can appear or not
If 0.9 can appear, so does .9
0

A regex like -?\d*(?:\.\d+)? causes the engine to find zero-width matches everywhere. A better approach is to simply use alternation to require that either there is a dot with numbers or one or more numbers after which a dot with more numbers is optional:

-?(?:\.\d+|\d+(?:\.\d+)?)

Regular expression visualization

Debuggex Demo

In Java you need to escape this like so:

Pattern p = Pattern.compile("-?(?:\\.\\d+|\\d+(?:\\.\\d+)?)");

Alternatively, you could add a lookahead that requires that there is at least one number somewhere:

-?(?=\.?\d)\d*(?:\.\d+)?

Regular expression visualization

Debuggex Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.