如何使用模式从字符串中获取字符串列表?

时间:2012-03-21 17:40:48

标签: c# regex string

我知道这个问题听起来有点奇怪。让我解释一下情况:

我有一个未定义的文本,如下所示:

Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo

您可能会注意到,文本中有一些占位符:{PlaceholderX}。我唯一知道的是占位符被{}包围。我不知道他们之间有什么关系。

现在我正在寻找从我的文本中获取{}所包围的所有字符串列表的最佳方法。

或者,为了使它更通用,是否有一种方法可以提供某种类型的模式,如{*}并将所有拟合的单词作为字符串取回?

7 个答案:

答案 0 :(得分:4)

您正在寻找正则表达式,在这种情况下,您需要使用lookarounds

(?<=\{)(.*)(?=\})

。*表示它会在大括号之间找到任何非空格字符

Here is a C# tutorial on how this can be used

Here is an example that shows how to pull out each item

我已根据你的例子调整了它

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
    // First we see the input string.
    string input = "Lorem {placeholder} ipsum {placeholder2} ...";

    // Here we call Regex.Match.
    Match match = Regex.Match(input, @"(?<=\{)(.*)(?=\})",
        RegexOptions.IgnoreCase);

    // Here we check the Match instance.
    if (match.Success)
    {
        // Finally, we get the Group value and display it.
        foreach(var matchgroup in match.Groups)
            Console.WriteLine(matchgroup.Value);
    }
    }
}

答案 1 :(得分:1)

您可以使用正则表达式。像这样:

string pattern = @"Your text with {placeholders} in it"
string[] placeholders = regex.Matches(input, @"\{\w+\}");

答案 2 :(得分:1)

Regex regex = new Regex("\{[^\}]+\}");
string[] matches = regex.Matches(text);

答案 3 :(得分:0)

您正在寻找Regular Expressions

答案 4 :(得分:0)

您可以使用以下代码,只需粘贴发布的答案中的任何正则表达式。根据占位符的格式化,有很多种可能性。

String s = "Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo";
Regex r = new Regex("{[a-zA-Z0-9]+}"); // Or any other regex found in one of the answers.
MatchCollection mc = r.Matches(s);
foreach (Match m in mc) {
    Console.WriteLine(m.Value);
}

确保您正在使用

using System.Text.RegularExpressions;

答案 5 :(得分:0)

string s = "Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo";
Regex regex = new Regex(@"{\w*}");
var temp= regex.Matches(s);
foreach(var item in temp)
{
     string key = item.ToString().Trim('{').Trim('}');
     Console.WriteLine(key);
}

答案 6 :(得分:0)

使用lazy-plus正则表达式模式。这将有效:

var txt =
    @"Lorem {placeholder1} ipsum dolor sit amet, consectetuer adipiscing elit. 
    Aenean commodo ligula eget dolor. Aenean massa. {placeholder2} Cum sociis 
    natoque penatibus et magnis dis parturient montes, {placeholder3} nascetur 
    ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium 
    quis, sem. Nulla consequat massa quis enim. Donec pede justo";

// need to do a lazy plus match...
var pattern = @"\{.+?\}";
var matches = Regex.Matches(txt, pattern);
foreach(Match match in matches)
{
    Console.WriteLine(match.Value);
}

输出

{placeholder1}
{placeholder2}
{placeholder3}

话虽如此,你有没有看过NVelocity