Home > Net >  Regular Expressions - multiple groups with separate condition
Regular Expressions - multiple groups with separate condition

Time:10-15

How can elements be designated to particular groups? How can certain parts be excluded so that they do not belong to any group? Is it possible to designate the exact element that should belong to a particular group?

Example:

Resume 191,44 €
First Element  131321312 290,60 € 59 142,00 € 28,00 € 0%
Second Elemnt  Name  131312313121 1014,79 € 19 140,00 € 28,00 € 0%
Third Element    118,80 € 19 25,00 € 5,00 € 0%
132123 Element Name    118,80 € 19 25,00 € 5,00 € 0%
Total : 12,00 €

Result:

 ────────────────────── ────────────── ─────────────── ────────────── ─────────────── ────────────── ────────────── 
| Element_Name         | first_value  | second_value  | third_value  | fourth_value  | fifth_value  | sixth_value  |
 ────────────────────── ────────────── ─────────────── ────────────── ─────────────── ────────────── ────────────── 
| Resume               | 191,44       | ""            | ""           | ""            | ""           | ""           |
| First Element        | 191,44       | ""            | ""           | ""            | ""           | ""           |
| Second Elemnt  Name  | 131312313121 | 1014,79       | 19           | 140,00        | 28,00        | 0            |
| Third Element        | ""           | 118,80        | 19           | 25,00         | 5,00         | 0            |
| 132123 Element Name  | ""           | 118,80        | 19           | 25,00         | 5,00         | 0            |
| Total                | 12,00        | ""            | ""           | ""            | ""           | ""           |
 ────────────────────── ────────────── ─────────────── ────────────── ─────────────── ────────────── ────────────── 

How I tried to do it: https://regex101.com/r/pvlKAF/1

While I could do this for those elements that have already fallen into expression, for the rest I do not know how to do it - the lines from "Resume", "Third Element" and the last two lines.

Is this to be done on an extra group basis? Is it even possible to do this from within the regex itself? (Such a division into groups that I described)

CodePudding user response:

With multiple optional groups you can use this long regex:

^(?P<Element_Name>(?:[a-zA-Z] |\d )(?:\s (?:[a-zA-Z] |\d ))*)[:\s] (?P<g1>\d (?:,\d )?)(?:[\s€] (?P<g2>\d (?:,\d )?)(?:[\s€] (?P<g3>\d (?:,\d )?)(?:[\s€] (?P<g4>\d (?:,\d )?)(?:[\s€] (?P<g5>\d (?:,\d )?)(?:[\s€] (?P<g6>\d (?:,\d )?))?)?)?)?)?[%€\s]*$

Updated RegEx Demo

  • Related