简体   繁体   English

正则表达式提取字符串

[英]Regex expression to extract strings

I have the following expression: 我有以下表达:

MN=Abc123,MN=sssa,MN=abc adsa 1,MN=&3ams d'amé,MN=dat,CB=ds,CB=ds MN = Abc123,MN = sssa,MN = abc adsa 1,MN =&ams d'amé,MN = dat,CB = ds,CB = ds

How can I extract one by one the expressions following MN= ? 我如何才能一一提取MN =之后的表达式? eg: firstly I want to extract Abc123 , secondly I wnat sssa and so on ... 例如:首先,我要提取Abc123 ,其次,我是wnat sssa ,依此类推...

Appreciate your answer! 感谢您的回答!

Use a capture group: 使用捕获组:

"[A-Z]{2}=([^,]+)"

Then get the first group form your matched object. 然后从匹配的对象中获得第一个组。

Or if the language you are dealing for supports the look-around you can use a positive look-behind in order to directly match the expected parts: 或者,如果您使用的语言支持环视,则可以使用正向后视以直接匹配期望的部分:

"(?<=[A-Z]{2}=)[^,]+"

If your regex environment supports lookbehind, you can extract desired information with this regex: 如果您的正则表达式环境支持向后查找,则可以使用此正则表达式提取所需的信息:

Environment supporting Lookbehind 环境向后支持

(?<=MN=)(.*?)(?=,) 

Environment not supporting Lookbehind 环境不支持向后看

(?:MN=)(.*?)(?=,)

Your desired result will be stored in Group 1, aka $1 您想要的结果将存储在第1组中,也就是$1

Based on your input string, here is result 根据您的输入字符串,这是结果

Abc123
sssa
abc adsa 1
&3ams d'amé
dat

See live demo here 在这里观看现场演示

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

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