C# Regex.Match 大括号-仅内容?(不包括大括号)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16537901/
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 01:16:35  来源:igfitidea点击:

C# Regex.Match curly brackets- contents only? (exclude braces)

c#.netregex

提问by PeterX

I've been unable to find an answer on this: can I use the Regex.Matchesmethod to return only the contentsof items with curly braces?

我一直无法找到答案:我可以使用该Regex.Matches方法仅返回带花括号的项目的内容吗?

If I use the Regex ({[^}]*})my MatchCollectionvalues includes the braces. I want to match, but then only return the contents. Here's what I have so far:

如果我使用正则表达式,({[^}]*})我的MatchCollection值包括大括号。我想匹配,但只返回内容。这是我到目前为止所拥有的:

Regex regex = new Regex(({[^}]*}), RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches("Test {Token1} {Token 2}");
// Results include braces (undesirable)
var results = matches.Cast<Match>().Select(m => m.Value).Distinct().ToList();

采纳答案by Milosz Krajewski

I always liked it explicit. So you can use "positive lookbehind" (?<=...) and "positive lookahead" (?=...) groups:

我一直喜欢它明确的。所以你可以使用“positive lookbehind” (?<=...) 和“positive lookahead” (?=...) 组:

(?<=\{)
[^}]*
(?=\})

which means:

意思是:

  • require opening curly bracket beforematch
  • collect text (of, course) - as commented before I may be [^{}]* as well
  • require closing curly bracket aftermatch
  • 需要匹配打开大括号
  • 收集文本(当然) - 正如我之前评论的那样,我也可能是 [^{}]*
  • 匹配需要关闭大括号

回答by RichieHindle

Just move the braces outside the parentheses:

只需将大括号移到括号外:

 {([^}]*)}

回答by UberMouse

If I understand what you want. Change the regex to {([^}]*)}. That will only capture the text between {}, not including them.

如果我明白你想要什么。将正则表达式更改为{([^}]*)}. 这只会捕获 {} 之间的文本,而不包括它们。

回答by bunjeeb

Thanks Milosz Krajewski, Nothing to add but here is the function

感谢 Milosz Krajewski,没什么可添加的,但这里是功能

private List<String> GetTokens(String str)
{
    Regex regex = new Regex(@"(?<=\{)[^}]*(?=\})", RegexOptions.IgnoreCase);
    MatchCollection matches = regex.Matches(str);

    // Results include braces (undesirable)
    return matches.Cast<Match>().Select(m => m.Value).Distinct().ToList();
}

回答by RajeshValangaiman

It is regex for C# .net.

它是 C# .net 的正则表达式。

@"{(.*?)}"

it display a

它显示一个

token1 token2

令牌1 令牌2

回答by Wiktor Stribi?ew

In C#, as in many other programming language, the regex engine supports capturing groups, that are submatches, parts of substrings that match a whole regex pattern, defined in a regex pattern with the help of parentheses (e.g. 1([0-9])3will match 123and save the value of 2into a capture group 1 buffer). Captured texts are accessed via Match.Groups[n].Valuewhere nis the index of the capture group inside the pattern.

在C#中,正如在许多其他的编程语言,正则表达式引擎支持捕获组,是子匹配,匹配整个正则表达式模式的子字符串的部分,用括号的帮助下,正则表达式模式定义(如1([0-9])3将匹配123和保存价值2进入捕获组 1 缓冲区)。捕获的文本通过以下方式访问,Match.Groups[n].Value其中n是模式内捕获组的索引。

Capturing is much more effecient that lookarounds. Whenever there is no need for complex conditions, capturing groups are much better alternatives.

捕获比环视更有效。当不需要复杂条件时,捕获组是更好的选择。

See my regex speed test performed at regexhero.net:

请参阅我在 regexhero.net 上执行的正则表达式速度测试:

enter image description here

在此处输入图片说明

Now, how can we get the substring inside curly braces?

现在,我们如何获得花括号内的子字符串

  • if there is no other curly braces inside, with a negated character class: {([^{}]*)
  • if there can be nested curly brackets: {((?>[^{}]+|{(?<c>)|}(?<-c>))*(?(c)(?!)))
  • 如果里面没有其他花括号,带有否定字符类{([^{}]*)
  • 如果可以嵌套大括号: {((?>[^{}]+|{(?<c>)|}(?<-c>))*(?(c)(?!)))

In both cases, we match an opening {, and then match (1) any character other than {or }, or (2) any characters up to the first paired }.

在这两种情况下,我们匹配开头{,然后匹配 (1) 除{or之外的任何字符},或 (2) 直到第一个配对的任何字符}

Here is sample code:

这是示例代码

var matches = Regex.Matches("Test {Token1} {Token 2}", @"{([^{}]*)");
var results = matches.Cast<Match>().Select(m => m.Groups[1].Value).Distinct().ToList();
Console.WriteLine(String.Join(", ", results));
matches = Regex.Matches("Test {Token1} {Token {2}}", @"{((?>[^{}]+|{(?<c>)|}(?<-c>))*(?(c)(?!)))");
results = matches.Cast<Match>().Select(m => m.Groups[1].Value).Distinct().ToList();
Console.WriteLine(String.Join(", ", results));

Result: Token1, Token 2, Token1, Token {2}.

结果:Token1, Token 2, Token1, Token {2}

Note that RegexOptions.IgnoreCaseis redundant when you have no literal letters that can have different case in the pattern.

请注意,RegexOptions.IgnoreCase当您没有可以在模式中具有不同大小写的文字字母时,这是多余的。

回答by sumit sharma

Little bit modifying the answer of @Milosz Krajewski

稍微修改@Milosz Krajewski 的答案

(?<=\{)[^}{]*(?=\})

this will skip the middle single opening and closing Curly braces in the string.

这将跳过字符串中中间的单个打开和关闭花括号。