Achraf Ben Salah
Achraf Ben Salah

Reputation: 276

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 :

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

Upvotes: 1

Views: 635

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

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.

Upvotes: 1

Related Questions