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

How to use regex to split by last occurrence of underscore

I want to split lines by last occurrence of underscore like this :

Input :

MANY_TIME_T1=PANORAMA17
MANY_TIME_T2=OK STATUS
MANY_TIME_T1=PANORAMA18
MANY_TIME_T2=OK STATUS
COMMENT=OK
LAST<LIGNE1

Output :

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

Match 1 : MANY_TIME_T1=PANORAMA17
Group 1 : MANY_TIME
Group 2 : T1
Group 3 : =
Group 4 : PANORAMA17

Match 2 : MANY_TIME_T2=OK STATUS
Group 1 : MANY_TIME
Group 2 : T2
Group 3 : =
Group 4 : OK STATUS

Match 3 : MANY_TIME_T1=PANORAMA18
Group 1 : MANY_TIME
Group 2 : T1
Group 3 : =
Group 4 : PANORAMA18

Match 4 : MANY_TIME_T2=OK STATUS
Group 1 : MANY_TIME
Group 2 : T2
Group 3 : =
Group 4 : OK STATUS

Match 5 : COMMENT=OK
Group 1 : COMMENT
Group 3 : =
Group 4 : OK

Match 6 : LAST<LIGNE1
Group 1 : LAST
Group 3 : <
Group 4 : LIGNE1

I try this regex ^\s*([^_<>=]+)(_\w+)?([<>=%])(.*)$ to split by underscore but it can’t split by last occurence like as above.

Demo

>Solution :

You can use

^\s*(\w+?)(?:_([^\W_]+))?([<>=%])(.*)

See the regex demo.

Details:

  • ^ – start of string
  • \s* – zero or more whitespaces
  • (\w+?) – Group 1: one or more word chars, as few as possible
  • (?:_([^\W_]+))? – an optional occurrence of _ and the one or more word chars except underscore captured into Group 2
  • ([<>=%]) – Group 3: one of the specified chars
  • (.*) – Group 4: the rest of the line.
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