1

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!!!

  • Based mostly on your examples it looks like you are asking for something like `[-+]?\d{1,3}(([.]\d{3})*([,]\d{1,2})?|([,]\d{3})*([.]\d{1,2})?)( €| EUR)?`. If it doesn't support all your cases then please provide more info about real formats you want to handle. – Pshemo Apr 17 '20 at 14:05
  • @Pshemo thanks for your help, but in your solution 06.01.99 will result in 3 "full matches" a=06, b=01, c=99 and I don't want one of these ;-) Wiktor Stribizew post a simple solution ;-) To my fault so easy, I should have figured it out myself... ;-) – knuspertante Apr 18 '20 at 15:48
  • Eliminating those groups is not a problem since we can use non-capturing groups instead of standard ones. Anyway what is the problem with having those groups? Existence of other groups can't stop us from using match of group 0 (unless that regex is menat to be part of bigger regex, which has its own groups. Then adding new groups may change indexes in other parts of real regex which we are not aware of). – Pshemo Apr 18 '20 at 16:58

1 Answers1

2

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563