Mukil Deepthi
Mukil Deepthi

Reputation: 6492

RegEx how to match this pattern

I am using the following regex to match the data as shown below. Only the 2nd and 3rd is matching not the first one. Can anyone help me know what am i doing wrong?

Basically i want to match the first and second and the last segments from the text.

Data:

306279 ABC TILT ROSE LVG
123456 ABC Chocolate PanL CH
123456 SSS Front Chocolate CH_Q

Expected Output

ProductNo   mattype     colorcode
---------   -------     ---------
306279      ABC         LVG
123456      ABC         CH
123456      SSS         CH_Q

My regex: Regex:

^(?<productno>\d{6})\s(?<mattype>\w+)\s(?<body>(?:(?![A-Z]{2}).)*)((?<colorcode>[A-??Z]{2}(?:_[A-Z])?)?)$

Thanks

Upvotes: 0

Views: 39

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 31035

You can use a regex like this:

(\d+)\s+(\w+).*\s(\w+)$

Working demo

Basing on your columns, you can have named groups like:

(?<ProductNo>\d+)\s+(?<mattype>\w+).*\s(?<colorcode>\w+)$

Regular expression visualization

Upvotes: 3

Related Questions