C# 从字符串中获取具有特定模式的子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14870551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:16:56  来源:igfitidea点击:

C# Get substring with specific pattern from string

c#.netstringlistsubstring

提问by bale3

I have a list of strings like this:

我有一个像这样的字符串列表:

List<string> list = new List<string>();
list.Add("Item 1: #item1#");
list.Add("Item 2: #item2#");
list.Add("Item 3: #item3#");

How can I get and add the substrings #item1#, #item2# etc into a new list?

如何获取子字符串#item1#、#item2# 等并将其添加到新列表中?

I am only able to get the complete string if it contains a "#" by doing this:

如果它包含“#”,我只能通过这样做来获取完整的字符串:

foreach (var item in list)
{
    if(item.Contains("#"))
    {
        //Add item to new list
    }
}

采纳答案by Botz3000

You could have a look at Regex.Match. If you know a little bit about regular expressions (in your case it would be a quite simple pattern: "#[^#]+#"), you can use it to extract all items starting and ending with '#'with any number of other characters other than '#'in between.

你可以看看Regex.Match。如果您对正则表达式略知一二(在您的情况下,这将是一个非常简单的模式:)"#[^#]+#",您可以使用它来提取'#'以任意数量的其他字符开头和结尾的所有项目,而不是'#'介于两者之间。

Example:

例子:

Match match = Regex.Match("Item 3: #item3#", "#[^#]+#");
if (match.Success) {
    Console.WriteLine(match.Captures[0].Value); // Will output "#item3#"
}

回答by yvesonline

How about this:

这个怎么样:

List<string> substring_list = new List<string>();
foreach (string item in list)
{
    int first = item.IndexOf("#");
    int second = item.IndexOf("#", first);
    substring_list.Add(item.Substring(first, second - first);
}

回答by Ash

You could do that by simply using:

您可以通过简单地使用:

    List<string> list2 = new List<string>();
    list.ForEach(x => list2.Add(x.Substring(x.IndexOf("#"), x.Length - x.IndexOf("#"))));

回答by masterlopau

try this.

尝试这个。

var itemList = new List<string>();
foreach(var text in list){
string item = text.Split(':')[1];
itemList.Add(item);


}

回答by masterlopau

LINQ would do the job nicely:

LINQ 可以很好地完成这项工作:

var newList = list.Select(s => '#' + s.Split('#')[1] + '#').ToList();

Or if you prefer query expressions:

或者,如果您更喜欢查询表达式:

var newList = (from s in list
               select '#' + s.Split('#')[1] + '#').ToList();

Alternatively, you can use regular expressions as suggested with Botz3000 and combine those with LINQ:

或者,您可以按照 Botz3000 的建议使用正则表达式,并将它们与 LINQ 结合使用:

var newList = new List(
    from match in list.Select(s => Regex.Match(s, "#[^#]+#"))
    where match.Success
    select match.Captures[0].Value
);

回答by Sergey Brunov

The code will solve your problem. But if the string does not contain#item#then the original string will be used.

该代码将解决您的问题。但如果字符串不包含,#item#则将使用原始字符串。

var inputList = new List<string>
    {
        "Item 1: #item1#",
        "Item 2: #item2#",
        "Item 3: #item3#",
        "Item 4: item4"
    };

var outputList = inputList
    .Select(item =>
        {
            int startPos = item.IndexOf('#');
            if (startPos < 0)
                return item;

            int endPos = item.IndexOf('#', startPos + 1);
            if (endPos < 0)
                return item;
            return item.Substring(startPos, endPos - startPos + 1);
        })
    .ToList();

回答by Jim D'Angelo

Here's another way using a regex with LINQ. (Not sure your exact requirements reference the regex, so now you may have two problems.)

这是在 LINQ 中使用正则表达式的另一种方法。(不确定您的确切要求是否引用了正则表达式,所以现在您可能有两个问题。)

var list = new List<string> ()
{
    "Item 1: #item1#",
    "Item 2: #item2#",
    "Item 3: #item3#",
    "Item 4: #item4#",
    "Item 5: #item5#",
};

var pattern = @"#[A-za-z0-9]*#";

list.Select (x => Regex.Match (x, pattern))
    .Where (x => x.Success)
    .Select (x => x.Value)
    .ToList ()
    .ForEach (Console.WriteLine);

Output:

输出:

#item1#

#item2#

#item3#

#item4#

#item5#

#it​​em1#

#it​​em2#

#it​​em3#

#it​​em4#

#it​​em5#