2

我正在开发一个通用应用程序,我需要匹配一个我无法解决的模式。

输入字符串可以是:

12_B 99-23_9_23

正如您在示例中看到的,我的问题是当我想匹配此模式“B 99-23”时。分隔符可以是任何东西,而不仅仅是下划线(例如 12|B 99-23|9|23)

现在这就是我所拥有的...

Regex r = new Regex("^(?< pol>\\w+)_(?< fac>\\w+)_(?< end>\\w+)_(?< op>\\w+)");

我必须更改这部分:(?< fac>\\w+) 模式必须将所有内容都保留到下一个分隔符 ('_'),包括空格、数字、字符。然后我会得到这个结果:

pol = 12
fac = B 99-23
end = 9
op = 23
4

2 回答 2

2

尝试使用这种模式:

^(?< pol>\w+)_(?< fac>[^_]+)_(?< end>\w+)_(?< op>\w+)

[^_]位是一个字符类,意思是“匹配除下划线以外的任何内容”。如果分隔符是|,则必须\|在模式中使用,因为|在正则表达式中具有特殊含义(尽管您不需要在字符类中对其进行转义)。像这样:

^(?< pol>\w+)\|(?< fac>[^|]+)\|(?< end>\w+)\|(?< op>\w+)

附带说明一下,我发现在指定正则表达式模式时使用逐字字符串要容易得多,因为您不必输入这么多转义序列:

new Regex(@"^(?< pol>\w+)\|(?< fac>[^|]+)\|(?< end>\w+)\|(?< op>\w+)");

但是,在这种情况下,您最好只使用Split

var result = input.Split(new char[] { '_' }, 4);
// result[0] = 12
// result[1] = B 99-23
// result[2] = 9
// result[3] = 23
于 2013-06-18T14:54:51.960 回答
0

描述

部分问题在于\w它还包括所有字母 az、所有数字 0-9 和下划线_。因此,如果您的输入文本可以使用_分隔符,那么表达式匹配\w+将变得混乱

因为您需要允许下划线作为分隔符,所以我建议\w您不要使用简写,而是为所需的文本和分隔符定义您想要的字符类。

  • [0-9a-zA-Z]+将以任何顺序匹配一个或多个字母或数字
  • [^a-zA-Z0-9]这是一个否定字符类,将匹配任何不是字母或数字的字符

此正则表达式将匹配所有值,并允许使用广泛的分隔符。

^(?<pol>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<fac>[0-9a-zA-Z]+\s[0-9a-zA-Z]+-[0-9a-zA-Z]+)[^a-zA-Z0-9](?<end>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<op>[0-9a-zA-Z]+)

在此处输入图像描述

团体

为了匹配fac组,我假设该字段将采用以下格式:字母空格数字连字符数字。

  • 第 0 组将获得整个匹配的字符串
  • 将创建命名组,但是在图像 1=pol、2=fac、3=end 和 4=op 中。很抱歉绘图软件无法处理命名的捕获组。

C# 代码示例:

输入文本

12_B 99-23_9_23
11_a 11-11_1_11
22|b 22-22|2|22
33-c 33-33-3-33
44,d 44-44,4,44

代码

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "source string to match with pattern";
          Regex re = new Regex(@"^(?<pol>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<fac>[0-9a-zA-Z]+\s[0-9a-zA-Z]+-[0-9a-zA-Z]+)[^a-zA-Z0-9](?<end>[0-9a-zA-Z]+)[^a-zA-Z0-9](?<op>[0-9a-zA-Z]+)",RegexOptions.IgnoreCase | RegexOptions.Multiline);
          MatchCollection mc = re.Matches(sourcestring);
          int mIdx=0;
          foreach (Match m in mc)
           {
            for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
              {
                Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
              }
            mIdx++;
          }
        }
    }
}

火柴

$matches Array:
(
    [0] => Array
        (
            [0] => 12_B 99-23_9_23
            [1] => 11_a 11-11_1_11
            [2] => 22|b 22-22|2|22
            [3] => 33-c 33-33-3-33
            [4] => 44,d 44-44,4,44
        )

    [pol] => Array
        (
            [0] => 12
            [1] => 11
            [2] => 22
            [3] => 33
            [4] => 44
        )


    [fac] => Array
        (
            [0] => B 99-23
            [1] => a 11-11
            [2] => b 22-22
            [3] => c 33-33
            [4] => d 44-44
        )


    [end] => Array
        (
            [0] => 9
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
        )


    [op] => Array
        (
            [0] => 23
            [1] => 11
            [2] => 22
            [3] => 33
            [4] => 44
        )


)
于 2013-06-19T03:40:44.550 回答