简体   繁体   English

C#正则表达式可选组不起作用

[英]C# regex optional group not working

I am trying to get parts from these strings: first: 我正在尝试从这些字符串中获取零件:首先:
2F4449534301224E4F204445534352495054494F4E20415641494C41424C45011F30303034342D30313230382D 2F4449534301224E4F204445534352495054494F4E20415641494C41424C45011F30303034342DD30313230382D
second: 第二:
2F4449534301224E4F204445534352495054494F4E20415641494C41424C45011F30303130312D3032323534012630303130312D31303932342D 2F4449534301224E4F204445534352495054494F4E20415641494C41424C45011F30303130312D3032323534012630303130312D31303932342D

basically I want to return for both strings: first: 基本上我想返回两个字符串:首先:
2F(.+)011F(.+)2D 2F(。+)011F(。+)2D
second: 第二:
2F(.+)011F(.+)0126(.+)2D 2F(。+)011F(。+)0126(。+)2D

I am trying to use this pattern: 我正在尝试使用这种模式:

Match m = Regex.Match(this.__line, 
                      @"^2F.*22(.*)011F(.*)(0126.*)?.{2}$", 
                      RegexOptions.IgnoreCase);

However, when I try: 但是,当我尝试:

if (m.Success)
{
    if (m.Groups[3].Value != "")
    {
        Console.WriteLine("good");
    }
}
else
{
    Console.WriteLine("bad");
}

I get "bad" from the second string because it is not matching the pattern. 我从第二个字符串中获取“错误”,因为它与模式不匹配。 Am I not using the correct pattern? 我使用的模式不正确吗?

The problem is that your pattern is greedy. 问题在于您的模式是贪婪的。 You should use this patten instead: 您应该改用该模板:

^2F.*22(.*?)011F(.*?)(0126.*?)?.{2}$

The second group in your regex matches everything until the last 2 charcaters at the end because it is greedy and the last group is optional. 正则表达式中的第二个组匹配所有内容,直到最后两个字符为止,因为它是贪婪的,最后一个组是可选的。

To make your matches nongreedy use a ? 要使您的比赛变得不贪心,请使用? after the quantifier. 在量词之后。

Here is more info about greedy and nongreedy. 是有关贪婪和贪婪的更多信息。

Hope this helps. 希望这可以帮助。

Take out the "^". 取出“ ^”。

2F. 2楼 22(. )011F(. )(0126. )?.{2}$ 22(。 )011F(。 )(0126。 )?。{2} $

http://regexpal.com/ is my hands down favorite regex tool. http://regexpal.com/是我最喜欢的正则表达式工具。

I would like to give you some advices. 我想给你一些建议。 These are not answers to your question , just some good practices tips : 这些不是您问题的答案 ,只是一些良好做法的提示

  • The anything but new line ( . ) symbol has a very poor performance, you should avoid using it whenever possible. 除换行 )符号外, 其他任何内容的性能都非常差,应尽可能避免使用它。 As I can see, you could replace it with \\S 如我所见,您可以将其替换为\\ S
  • For case insensitive match, use the syntax (?i:pattern) . 对于不区分大小写的匹配,请使用语法(?i:pattern) This gives you the option of choosing RegexOptions.Compiled , which will give you a better performance 这使您可以选择RegexOptions.Compiled ,这将为您提供更好的性能
  • For retrieving text, the use of named capture groups is recommended. 为了检索文本,建议使用命名捕获组。 Use the syntax (?<name>pattern) . 使用语法(?<name>pattern) This way you can retrieve it by regexMatch.Groups["name"].Captures[0].Value 这样,您可以通过regexMatch.Groups["name"].Captures[0].Value检索它
  • Whenever you have a group that you do not want to retrive (only for matching purposes), mark it as a non-capturing group, using the syntax (?:pattern) 每当您有不想检索的组(仅用于匹配目的)时,请使用语法(?:pattern)将其标记为非捕获组。
  • Lastly, RegexBuddy is a great (yet paid) tool. 最后, RegexBuddy是一个很棒的工具(尚未付款)。 Highly recommended. 强烈推荐。

Regards. 问候。

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

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