获取两个多字符定界符之间的最短子字符串 [英] Get the shortest substrings between two multicharacter delimiters

查看:41
本文介绍了获取两个多字符定界符之间的最短子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

 字符串文本="aa aa值kk 8718764 aa值1 kk kk kk 5178gkjh aathtkhkk"; 

我想获取 aa kk 之间的所有文本,并且预期的结果是:

  1 =值2 =值13 =吨 

我尝试使用"aa(.*?)kk" 正则表达式,但是没有得到预期的结果.

解决方案

.*?仍将在 aa kk .

使用

C#代码:

  var re = @"aa((?:( ?! aa).)*?)kk";var str ="aa aa值kk 8718764 aa值1 kk kk kk 5178gkjh aathtkhkk";var res = Regex.Matches(str,re).Cast< Match>().Select(p => p.Groups [1] .Value).ToList(); 

I have

string text = "aa aa value kk 8718764 aa value1 kk kk kk 5178gkjh aathtkhkk";

I want to get all texts between aa and kk and the expected results are:

1 = value
2 = value1
3 = thtkh

I try using a "aa(.*?)kk" regex, but I am not getting the expected result.

解决方案

The .*? will still match aa in between aa and kk.

Use a tempered greedy token:

aa((?:(?!aa).)*?)kk
   ^^^^^^^^^^^^^

or

aa((?:(?!aa|kk).)*)kk
   ^^^^^^^^^^^^^^^

See the regex demo

Details:

  • aa - an aa substring
  • ((?:(?!aa).)*?) - Group 1 capturing any zero or more chars (if RegexOptions.Singleline option used, even including newline) that are not starting an aa substring sequence, as few as possible
  • kk - a kk substring

C# code:

var re = @"aa((?:(?!aa).)*?)kk";
var str = "aa aa value kk 8718764 aa value1 kk kk kk 5178gkjh aathtkhkk"; 
var res = Regex.Matches(str, re)
    .Cast<Match>()
    .Select(p => p.Groups[1].Value)
    .ToList();

这篇关于获取两个多字符定界符之间的最短子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆