简体   繁体   English

从长字符串正则表达式中获取匹配的子字符串

[英]get matching substrings from long string Regex

I have a string like this 我有这样的字符串

   jasabcasjlabcdjjakabcdehahakabcdef...//any number of characters

I want regex that returns these substrings 我想要返回这些子字符串的正则表达式

  [abc],[abcd],[abcde],[abcdef],....

I have written regex something like this 我写了这样的正则表达式

 @"abc(?=[d-z])+

But it's not bringing what I want, I have been trying for some time, please help 但这并没有带来我想要的东西,我已经尝试了一段时间,请帮助

Thanks 谢谢

Approach with a foreach -loop 使用foreach循环的方法

string input = "jasabcasjlabcdjjakabcdehahakabcdef";

List<string> result = new List<string>();
string temp = string.Empty;

foreach(char c in input)
{
    if(c == 'a' && temp == string.Empty)
    {
        temp = string.Empty;
        temp += c;                
    }
    else if(c - 1  == temp.LastOrDefault())
    {
        temp += c;                  
    }
    else if (!string.IsNullOrEmpty(temp))
    {
        if (temp.StartsWith("abc"))
        {
            result.Add(temp);
        }
        temp = string.Empty;
    }
}
if (temp.StartsWith("abc"))
{
    result.Add(temp);
}

https://dotnetfiddle.net/I4t9Cq https://dotnetfiddle.net/I4t9Cq

Linq approach Linq方法

string input = "jasabcasjlabcdjjakabcdehahakabcdef";
string[] result = Regex.Split(input, @"(?=abc)")
                       .Select(x => string.Concat(x.TakeWhile((y, i) => y == ('a' + i))))
                       .Where(x => !string.IsNullOrEmpty(x))
                       .ToArray();

https://dotnetfiddle.net/tahJ4U https://dotnetfiddle.net/tahJ4U

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

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