【发布时间】:2016-08-13 13:49:30
【问题描述】:
我之前已经发布了这个,但没有给出我想要达到的目标的明确信息。
我正在尝试在 c# 中使用正则表达式从字符串中获取值。我无法理解为什么有些值我可以得到而有些我不能使用类似的方法。
请在下面找到代码 sn-p。 请让我知道我错过了什么。 提前致谢。
string text = "0*MAO-001*20160409*20160408*Encounter Data Duplicates Report * *ENC000200800400120160407*PRO*PROD*";
//toget the value 20160409 from the above text
//this code works fine
Regex pattern = new Regex(@"([0][*]MAO[-][0][0][1].*?[*](?<Value>\d+)[*])");
Match match = pattern.Match(text);
string Value = match.Groups["Value"].Value.ToString();
//to get the value ENC000200800400120160407 from the above text
// this does not work and gives me nothing
Regex pattern2 = new Regex(@"([0][*]MAO[-][0][0][1].*?[*].*?[*].*?[*].*?[*].*?[*](?<Value2>\d+)[*])");
Match match2 = pattern.Match(text);
string Value2 = match.Groups["Value2"].Value.ToString();
【问题讨论】:
-
尝试第二个正则表达式:-
([0][*]MAO[-][0][0][1].*?[*].*?[*].*?[*].*?[*].*?[*](?<Value2>\w+)[*]) -
哇.. 谢谢.. 它有效.. 你想把它作为答案发布.. 什么是“w+”?
-
您应该在
*上拆分,而不是使用可能遗漏某些内容的特定字段验证。
标签: c# .net regex string match