Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular Expressions – two different occurrence in one group per line (group in group)

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading