Home > Enterprise >  Regular Expressions - two different occurrence in one group per line (group in group)
Regular Expressions - two different occurrence in one group per line (group in group)

Time:10-14

Is it possible to get a specific value from the group?

Example:

TEST TEST 0,00 € 23,00 €
TEST TEST 1219,51 €
TEST TEST 50,11 €
TEST 1,11 €
TEST TEST TEST 313,31 €
TEST 123,13 € 50,23 €
TEST TEST TEST TEST 313,13 €
TEST 13,23 €

expected resaults:


TEST TEST 0,00 € 23,00 € <-- Group1 0,00 Group2 23,00
TEST TEST 1219,51 € <-- Group1 1219,51
TEST TEST 50,11 € <-- Group1 50,11
TEST 1,11 € <-- Group1 1,11
TEST TEST TEST 313,31 € <-- Group1 313,31
TEST 123,13 € 50,23 € <-- Group1 123,13 <-- Group2 50,23
TEST TEST TEST TEST 313,13 € <-- Group1 313,13
TEST 13,23 € <-- Group1 13,23

I tried to do it this way: (?P<first_price>\d ,\d )|(?P<second_price>.*\d ,\d .*\d ,\d )

But I have trouble with dividing these numbers within the second group https://regex101.com/r/HD2qjh/1

Is it possible to divide it like that within one regular expression and some additional operators?

CodePudding user response:

You may use this regex:

(?P<Group1>\d ,\d )(?:[^\d\n]*(?P<Group2>\d ,\d ))?

Updated Regex Demo

RegEx Details:

  • (?P<Group1>\d ,\d ): Named capture group Group1 to match and capture 1 digit then comma then 1 digit
  • (?:: Start a non-capture group
    • [^\d\n]*: Match 0 or more of any char that is not a digit and not a line break
    • (?P<Group2>\d ,\d ): Named capture group Group2 to match and capture 1 digit then comma then 1 digit
  • )?: End non-capture group. ? makes this group optional
  • Related