正则表达式 +图案中包含一个空格

发布于 2024-12-19 10:38:15 字数 350 浏览 2 评论 0原文

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

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

“*”代表“Z”之前数字的通配符,但它似乎不想使用其中的空间。例如,我可以成功使用以下模式来匹配相同的内容,而无需空格(即 3Z5Z),

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

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

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

I'm trying to figure out how to write a pattern to match to the following: "3Z 5Z". The numbers in this can vary, but the Z's are constant. The issue I'm having is trying to include the white space... Currently I have this as my pattern

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

The '*' represent the wildcard for the number preceding the "Z", but it doesn't seem to want to work with the space in it. For example, I can use the following pattern successfully for matching to the same thing without the space (i.e. 3Z5Z)

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

I am writing this program in .NET 4.0 (C#). Any help is much appreciated!

EDIT: This pattern is part of a larger string, for example:
3Z 10Z lock 425"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夢归不見 2024-12-26 10:38:16

试试这个:

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 是单词锚点。你无法量化它。

Try this:

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

Explanation:

"
\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
"

By the way:

\b*

Should throw an exception. \b is a word anchor. You can't quantify it.

野却迷人 2024-12-26 10:38:16

试试这个代码。

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();
    }
  }
}

Try this code.

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();
    }
  }
}
尽揽少女心 2024-12-26 10:38:16

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

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

I addition to other posts I would add characters of the Begin and End of string.

patter = "^\d+Z\s\d+Z$"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文