javaregexregex-group

Regular expression to get a specific group


I have this expression: (\D)((-?|\+?)\d+(\.\d+)?)? This work for the most of the patterns i get. But, exists this pattern in the string that a i don't be able to get at all. I'm using Java with spring boot

This is the string that a apply the regex:

$1.0?8S0.0H692.8Q651.7u3.3o294.8n0t32.6s0R0.0P0.0Y114.9|136.3%1N-22.08930501E-48.05973392D692.374U0.0V0.00O0.0I6G0.20F12h0.000v0.000x1y1a24.0412e5.0690b0.00l0.0000r0.0000f0p4¨0.0@0.0c0.0d0.0º0|0|0|0=51.9988B1.0#0

It's is simply, get the first character that's not a digit, after get the plus or minus character, after get the number. if exists a float point, get the point with the remnant numbers. This is the common pattern. The result is something like:

After a put this in a HashMap.

But there is a part that i have more than one number to get, separated by pipe :

Because of the firts rule, this is interpreted as another value.

I want to change the regex to deal with this specific rule. To get something like this:

After this i will develop a logic to put this in an array

Thank's to anyone for the help


Solution

  • You could change \D (which would match any character except a digit) to a more specific negated character class [^\d\s.+-] excluding more characters.

    For the part with the repeated pipe | char and the digits, you can use optionally repeating non capture group.

    If you don't want to get the individual parts, you can omit the capture groups at all and just get the full match.

    You could use:

    [^\d\s.+-][-+]?\d+(?:\.\d+)?(?:\|\d+(?:\.\d+)?)*
    

    The pattern in parts match:

    See a regex demo