【问题标题】:Regular Expressions + Including one space in pattern正则表达式 + 在模式中包含一个空格
【发布时间】:2012-01-10 21:52:49
【问题描述】:

我试图弄清楚如何编写一个模式来匹配以下内容:“3Z 5Z”。其中的数字可能会有所不同,但 Z 是恒定的。我遇到的问题是试图包含空格......目前我有这个作为我的模式

 pattern = @"\b*Z\s*Z\b";

“*”代表“Z”前面的数字的通配符,但它似乎不想使用其中的空格。例如,我可以成功使用以下模式匹配没有空格的相同事物(即 3Z5Z)

pattern = @"\b*Z*Z\b";

我正在用 .NET 4.0 (C#) 编写这个程序。非常感谢任何帮助!

编辑:此模式是较大字符串的一部分,例如: 3Z 10Z 锁425"

【问题讨论】:

    标签: c# arrays regex function output


    【解决方案1】:

    试试这个:

    pattern = @"\b\d+Z\s+\d+Z\b";
    

    说明:

    "
    \b    # Assert position at a word boundary
    \d    # Match a single digit 0..9
       +     # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
    Z     # Match the character “Z” literally
    \s    # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
       +     # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
    \d    # Match a single digit 0..9
       +     # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
    Z     # Match the character “Z” literally
    \b    # Assert position at a word boundary
    "
    

    顺便说一句:

    \b*
    

    应该抛出异常。 \b 是一个词锚。你无法量化它。

    【讨论】:

    • 嗨 FailedDev - 感谢您的建议,但它仍然没有返回匹配项。也许您可以发现错误 - 我现在有:pattern = @"\b\d+Z\s+\d+ Z\b"; (编辑 - 删除 astrix 但仍然无法正常工作)
    • @keynesiancross \d+* 这也应该引发异常。去掉 + 后面的星号。
    • 抱歉 - 删除了 astrix(参见上面的编辑)但仍然不提供匹配...还添加了对 OP 的编辑
    • 原来我只是个白痴,在方法运行之前设置了断点。你上面的代码效果很好。非常感谢
    【解决方案2】:

    试试这个代码。

    using System;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          string txt="3Z 5Z";
    
          string re1="(\\d+)";  // Integer Number 1
          string re2="(Z)"; // Any Single Character 1
          string re3="( )"; // Any Single Character 2
          string re4="(\\d+)";  // Integer Number 2
          string re5="(Z)"; // Any Single Character 3
    
          Regex r = new Regex(re1+re2+re3+re4+re5,RegexOptions.IgnoreCase|RegexOptions.Singleline);
          Match m = r.Match(txt);
          if (m.Success)
          {
                String int1=m.Groups[1].ToString();
                String c1=m.Groups[2].ToString();
                String c2=m.Groups[3].ToString();
                String int2=m.Groups[4].ToString();
                String c3=m.Groups[5].ToString();
                Console.Write("("+int1.ToString()+")"+"("+c1.ToString()+")"+"("+c2.ToString()+")"+"("+int2.ToString()+")"+"("+c3.ToString()+")"+"\n");
          }
          Console.ReadLine();
        }
      }
    }
    

    【讨论】:

    • 它匹配 Z 和任何整数。
    • 仅供参考,Singleline 选项在这里无效。它所做的只是让点匹配任何字符包括换行,并且这个正则表达式中没有换行。
    【解决方案3】:

    除了其他帖子之外,我还会添加字符串开头和结尾的字符。

    patter = "^\d+Z\s\d+Z$"
    

    【讨论】:

    • 是的,但是 OP 有单词锚,所以这个字符串可能是更大字符串的一部分。
    猜你喜欢
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多