简体   繁体   English

通过正则表达式提取相同字符串之间的内容

[英]Extract content between same strings via Regex

I would like to capture everything between 2 same strings. 我想捕获2个相同字符串之间的所有内容。

String1: two
String2: two

EXAMPLE: 例:

two strings two 123 twoIKStwo .[]?>-=two&1`ddddtwo()dsaltwo 

RESULT: 结果:

 strings , 
 123 ,
IKS,
 .[]?>-=,
&1`dddd,
()dsal

*including line-break and any invisible characters. *包括换行符和任何不可见的字符。

I have tried several Regexes but I've failed miserably. 我已经尝试了多个正则表达式,但失败了。 I would like to capture one group results if possible, and purely in Regex syntax. 我想尽可能地捕获一组结果,并且仅使用正则表达式语法。 I want to capture everything inside I have searched across the web, but it seems nobody needs to accomplish this or second option is that it is extremely easy to do it, so nobody is asking for that. 我想捕获我在网上搜索过的所有内容,但是似乎没有人需要完成此操作,第二种选择是这样做非常容易,因此没有人要求这样做。

Thank you for any suggestions. 感谢您的任何建议。

You can use below regex: 您可以使用以下正则表达式:

([A-Za-z]+)(.*?)(?=\1)

And then capture group 2. 然后捕获组2。

Output: 输出:

Match 1
Group 2.    3-12    ` strings `
Match 2
Group 2.    15-20   ` 123 `
Match 3
Group 2.    23-26   `IKS`
Match 4
Group 2.    29-37   ` .[]?>-=`
Match 5
Group 2.    40-47   `&1`dddd`
Match 6
Group 2.    50-56   `()dsal`

Demo: https://regex101.com/r/wJ7hW5/1 演示: https : //regex101.com/r/wJ7hW5/1

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

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