Search code examples
javaregexregex-lookaroundsregex-group

regexp negative lookahead with groups


I try to use regexp with negative lookahead and with groups. Unfortunately it does not work as expected, so I need a helping hand.

I need to parse out the sizes (inches) at the end of a string which can be the format 'Tony Hawk 39.75"' --> 39.75" or with a length x width 'Tony Hawk 39.75" x 8.75"' --> 9.75" x 8.75". The problem is that the name also can contain numbers e.g. 'AD-37 8.0"'. So I tried with negative lookahead that a 'space' does not follow an 'x' (?!.+\s[x]) but it comes with strange result back.

Pattern regex = Pattern.compile("(?!.+\\s[x])\\s(\\d+\\.?\\d*\".*)");
String s = "Tony Hawk 39.75\" x 8.75\"";
// or String s = "AD-37 8.0\"";
Matcher m = regex.matcher(s);
if (m.find()) {
    System.out.println(m.groupCount());
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

When I execute the example above it returns me for group 1 '8.75"', which I don't understand. Here I would expect 'Tony Hawk'.

Any hints what I did wrong?


Solution

  • Using the pattern:

    (.+?)\\s(\\d+\\.\\d*\".*)
    

    Might yield better results:

    2 
    Tony Hawk
    39.75" x 8.75"
    

    Example:

    https://regex101.com/r/pN0fW1/1