尝试编写与[ID] ='anynumber'匹配的正则表达式

时间:2019-03-28 09:28:47

标签: c# regex string

我正在尝试编写与子字符串“ [ID] ='#number'”相匹配的正则表达式。

这是我意思是字符串的一部分:

[Patient.ID] = 123 And [Zorgtraject] = 123 And [Activiteit] Is Null Or [ID] = '86777' Or [ID] = '86778' Or [ID] = '86779' Or [ID] = '86780' Or [ID] = '86781' Or [ID] = '86782' Or [ID] = '86783' Or [ID] = '86784' Or [ID] = '86785' Or [ID] = '86786' Or [ID] = '86787' Or [ID] = '86788' Or [ID] = '86789' Or [ID] = '86790' Or [ID] = '86791' Or [ID] = '86792' Or [ID] = '86793' Or [ID] = '86794' Or [ID] = '86795' Or [ID] = '86796' Or [ID] = '86797' Or [ID] = '86798' Or [ID] = '86799' Or [ID] = '86800' Or [ID] = '86801' Or [ID] = '86802' Or [ID] = '86803' Or [ID] = '86804' Or [ID] = '86805' Or [ID] = '86806' Or [ID] = '86807' Or [ID] = '86808' Or [ID] = '86809' Or [ID] = '86810' Or [ID] = '86811' Or [ID] = '86812' Or [ID] = '86813' Or [ID] = '86814' Or [ID] = '86815' Or [ID] = '86816' Or [ID] = '86817' Or [ID] = '86818' Or [ID] = '86819' Or [ID] = '86820' Or [ID] = '86821' Or [ID] = '86822' Or [ID] = '86823' Or [ID] = '86824' Or [ID] = '86825' Or [ID] = '86826' Or [ID] = '86827' Or [ID] = '86828' Or [ID] = '86829' Or [ID] = '86830' Or [ID] = '86831' Or [ID] = '86832' Or [ID] = '86833' Or [ID] = '86834' Or [ID] = '86835' Or [ID] = '86836' Or [ID] = '86837' Or [ID] = '86838' Or [ID] = '86839' Or [ID] = '86840' Or [ID] = '86841' Or [ID] = '86842' Or [ID] = '86843' Or [ID] = '86844' Or [ID] = '86845' Or [ID] = '86846' Or [ID] = '86847'

我已经走了这么远:[\[ID\] = '{\d+:\d+}'],但是它匹配得很奇怪,就像它还从[中抢走了ID]Patient.ID

我想要的是一个正则表达式,它使子字符串像[ID] = '86845'

谢谢。

1 个答案:

答案 0 :(得分:5)

您可以使用

\[ID]\s*=\s*'\d+'

请参见regex demo

详细信息

  • \[ID]-[ID]子字符串
  • \s*=\s*-一个=内含0+个空格字符
  • '-单引号
  • \d+-1个以上数字
  • '-单引号

Regulex graph

enter image description here

C#:

var results = Regex.Matches(s, @"\[ID]\s*=\s*'\d+'")
        .Cast<Match>()
        .Select(x => x.Value)
        .ToList();

要仅获取数字,请使用

var results = Regex.Matches(s, @"\[ID]\s*=\s*'(\d+)'")
        .Cast<Match>()
        .Select(x => x.Groups[1].Value)
        .ToList();