将匹配结果从regex转换为字符串列表

时间:2022-09-13 16:48:08

How can I convert the list of match result from regex into List<string>? I have this function but it always generate an exception,

如何将regex中的匹配结果列表转换为list ?我有这个函数但它总是会产生一个异常,

Unable to cast object of type 'System.Text.RegularExpressions.Match' to type 'System.Text.RegularExpressions.CaptureCollection'.

无法强制类型为System.Text.RegularExpressions的对象。“输入”System.Text.RegularExpressions.CaptureCollection”相匹配。

public static List<string> ExtractMatch(string content, string pattern)
{
    List<string> _returnValue = new List<string>();
    Match _matchList = Regex.Match(content, pattern);
    while (_matchList.Success)
    {
        foreach (Group _group in _matchList.Groups)
        {
            foreach (CaptureCollection _captures in _group.Captures) // error
            {
                foreach (Capture _cap in _captures)
                {
                    _returnValue.Add(_cap.ToString());
                }
            }
        }
    }
    return _returnValue;
}

If I have this string,

如果我有这条线,

I have a dog and a cat.

regex

正则表达式

dog|cat

I want that the function will return of result into List<string>

我希望函数将结果返回到List

dog
cat

4 个解决方案

#1


72  

With the Regex you have, you need to use Regex.Matches to get the final list of strings like you want:

有了Regex,您需要使用Regex。匹配以获得您想要的字符串的最终列表:

MatchCollection matchList = Regex.Matches(Content, Pattern);
var list = matchList.Cast<Match>().Select(match => match.Value).ToList();

#2


9  

Cross-posting answer from Looping through Regex Matches --

交叉发布的答案从循环通过Regex匹配—。

To get just a list of Regex matches, you may:

要获得Regex匹配的列表,您可以:

var lookfor = @"something (with) multiple (pattern) (groups)";
var found = Regex.Matches(source, lookfor, regexoptions);
var captured = found
                // linq-ify into list
                .Cast<Match>()
                // flatten to single list
                .SelectMany(o =>
                    // linq-ify
                    o.Groups.Cast<Capture>()
                        // don't need the pattern
                        .Skip(1)
                        // select what you wanted
                        .Select(c => c.Value));

This will "flatten" all the captured values down to a single list. To maintain capture groups, use Select rather than SelectMany to get a list of lists.

这将使所有捕获的值“平坦化”为一个列表。要维护捕获组,使用Select而不是SelectMany来获取列表列表。

#3


3  

A possible solution using Linq:

使用Linq的一种可能的解决方案:

using System.Linq;
using System.Text.RegularExpressions;

static class Program {
    static void Main(string[] aargs) {
        string value = "I have a dog and a cat.";
        Regex regex = new Regex("dog|cat");
        var matchesList = (from Match m in regex.Matches(value) select m.Value).ToList();
    }
}

#4


1  

Here's another solution that will fit into your code well.

这是另一个适合您的代码的解决方案。

while (_matchList.Success)
{
    _returnValue.Add(_matchList.Value);
    _matchList = _matchList.NextMatch();
}

#1


72  

With the Regex you have, you need to use Regex.Matches to get the final list of strings like you want:

有了Regex,您需要使用Regex。匹配以获得您想要的字符串的最终列表:

MatchCollection matchList = Regex.Matches(Content, Pattern);
var list = matchList.Cast<Match>().Select(match => match.Value).ToList();

#2


9  

Cross-posting answer from Looping through Regex Matches --

交叉发布的答案从循环通过Regex匹配—。

To get just a list of Regex matches, you may:

要获得Regex匹配的列表,您可以:

var lookfor = @"something (with) multiple (pattern) (groups)";
var found = Regex.Matches(source, lookfor, regexoptions);
var captured = found
                // linq-ify into list
                .Cast<Match>()
                // flatten to single list
                .SelectMany(o =>
                    // linq-ify
                    o.Groups.Cast<Capture>()
                        // don't need the pattern
                        .Skip(1)
                        // select what you wanted
                        .Select(c => c.Value));

This will "flatten" all the captured values down to a single list. To maintain capture groups, use Select rather than SelectMany to get a list of lists.

这将使所有捕获的值“平坦化”为一个列表。要维护捕获组,使用Select而不是SelectMany来获取列表列表。

#3


3  

A possible solution using Linq:

使用Linq的一种可能的解决方案:

using System.Linq;
using System.Text.RegularExpressions;

static class Program {
    static void Main(string[] aargs) {
        string value = "I have a dog and a cat.";
        Regex regex = new Regex("dog|cat");
        var matchesList = (from Match m in regex.Matches(value) select m.Value).ToList();
    }
}

#4


1  

Here's another solution that will fit into your code well.

这是另一个适合您的代码的解决方案。

while (_matchList.Success)
{
    _returnValue.Add(_matchList.Value);
    _matchList = _matchList.NextMatch();
}