繁体   English   中英

正则表达式或拆分逻辑以在C#中的花括号之间创建文本数组

[英]Regex or split logic to create array of text between curly braces in C#

我有一些字符串,需要在花括号之间提取文本并将其分配给字符串数组。

字符串之一的示例是:

select * from cable where exchange like '%{Enter Exchange:}%' and Type like '%{Enter Type:}%'

基于此示例,我要提取Enter Exchange:Enter Type:

注意:这是一个仅包含两组{}的示例。 可能会有更多或更少。

这可能很简单,但是我对Regex不太熟悉,而且很难拆分字符串。 如果在其他地方有指向类似问题的链接,我们将很乐意进行检查。

谢谢。

[TestMethod]
public void TestRegex()
{
    var input = "select * from cable where exchange like " +
                "'%{Enter Exchange:}%' and Type like '%{Enter Type:}%'";

    var result = Regex.Matches(input, @"%\{(.+?)\}%")
            .Cast<Match>()
            .Select(m => m.Groups[1].Value)
            .ToArray();

    result.Should().HaveCount(2);
    result.Should().Contain("Enter Exchange:");
    result.Should().Contain("Enter Type:");
}

此正则表达式将匹配{word}模式

\\{.+?\\}

暂无
暂无

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

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