简体   繁体   English

RegEx如何匹配此模式

[英]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. 只有第2个和第3个不匹配第一个。 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 谢谢

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+)$

正则表达式可视化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM