Search code examples
javaregexregex-lookarounds

Java Regex Lookahead Conditional


I have a regex which works, but unfortunately not in Java because Java does not support this type of inline modifier.

I have already read about this topic e. g. here:

My regex:

(?(?=\d{1,2}[.]\d{1,2}[.]\d{2,4})somerandomtextwhichisnotinthetext|^((($|EUR)? ?[-+]?(\d{1,8}[.,])*\d+([.,]\d+)?)|([-+]?(\d{1,8}[.,])*\d+([.,]\d+)? ?($|€|EUR)?))$)

I also tried a lookbehind but the pattern it should be matched has a variable length an this is unfortunately not supported...

The regex should me matches all of this pattern (a full match is needed --> matcher.group(0) ):

  • 123.342,22
  • 123,233.22
  • 232,11
  • 232.2
  • 232.2 €

but not this:

  • 06.01.99

And it needs to be implemented in Java.

But still I have no solution...

Thanks for your help!!!


Solution

  • The point here is that you need to use the first part as a negative lookahead to add an exception to the other pattern:

    ^(?!\d{1,2}[.]\d{1,2}[.]\d{2,4}$)((($|EUR)? ?[-+]?(\d{1,8}[.,])*\d+([.,]\d+)?)|([-+]?(\d{1,8}[.,])*\d+([.,]\d+)? ?($|€|EUR)?))$
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    See the regex demo

    So, rather than requiring an exception pattern and then failing to match a fake string, it makes sense to simply use a valid match pattern and add an exception at the start.

    I also see ($|€|EUR)?, you probably wanted to match a dollar symbol here. If I am right, replace it with ([$€]|EUR)?. Also, ($|EUR)? might also need replacing with ([$€]|EUR)?.

    Also, consider using non-capturing groups rather than capturing ones, since you say you are only interested in full match values.