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

Regex alphanumeric dynamic block grouping

I need your help because I have tried in several ways to create a regular expression which I can get by groups but I have been having trouble with a variation which I don’t know how to take into account:

Words:

589011 T CANNELLE FLAVOR 8
538160 TP0597C ORANGE FLAVOR
557137 APL0397C STRAWBERRY FLAVOR
556137 APL0397C STRAWBERRY FLAVOR
545129 1APL0397C MANGO FLAVOR
984320 TRANS DECENAL
533160 TP0597C ORANGE FLAVOR
530373 A PINEAPPLE FLAVOR
059311 A STRAWBERRY FLAVOR
508142 T LEMON FLAVOR
547261 A BLACKBERRY FLAVOR
536564 2T BLUEBERRY STRAWBERRY
055406 A MILK FLAVOR
054945 A BLACKBERRY FLAVOR
983347 SUSTANE
902040 ACETIC ACID

Groups 1:

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

589011 T
538160 TP0597C
557137 APL0397C
556137 APL0397C
545129 1APL0397C
984320
533160 TP0597C
530373 A
059311 A
508142 T
547261 A
536564 2T
055406 A
054945 A
983347
902040

Groups 2:

CANNELLE FLAVOR
ORANGE FLAVOR
STRAWBERRY FLAVOR
STRAWBERRY FLAVOR
MANGO FLAVOR
TRANS DECENAL
ORANGE FLAVOR
PINEAPPLE FLAVOR
STRAWBERRY FLAVOR
LEMON FLAVOR
BLACKBERRY FLAVOR
BLUEBERRY STRAWBERRY
MILK FLAVOR
BLACKBERRY FLAVOR
SUSTANE
ACETIC ACID

Regex Test:

  1. (\d+\s?[A-Z0-9]*)\s+([A-Z\s]+)\s
  2. ^([0-9\s]+(?:[A-Z0-9]+)?)\s(?:[A-Z\s]+)

The difficulty I have with these two words is that the groups should be like this:

988320 TRANS DECENAL 1 KGM 12164500 JC4 20.0000 KILO 263.98/KG 5,279.60
903040 ACETIC ACID 1 KGM 12164500 DJ4 100.0000 KILO 3.17/KG 317.00

Group 1:

988320
903040

Group 2:

TRANS DECENAL
ACETIC ACID

and not like that:

Group 1:

988320 TRANS
903040 ACETIC

Group 2:

DECENAL
ACID

>Solution :

^(\d+\s?\b(?:[A-Z]|[A-Z]*\d[A-Z\d]*)?\b)\s*([A-Z ]*\b)

Group 1 matches digits followed optionally by either a word with a single letter, or a word that contains a mixture of letters and digits with at least one digit. Group 2 gets all the words with just letters after that, except for the whitespace between them.

DEMO

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