[英]c# Regular Expression no match - when there should be
I'm trying to match 0 or more strings of letters, numbers, and white space between quotation marks ("), these sets can be separated by white space or not, and the string will start with a key word to identify what to do with the matched sets. 我正在尝试将0个或多个字母,数字和引号(“)之间的空格匹配,这些集合可以用空格隔开,也可以不使用空格,并且字符串将以关键字开头以标识要做什么与匹配的集合。
The simplest example is below: 最简单的示例如下:
\\ inString = test "1" "a""3";
Regex regEx = new Regex("@(\"[0-9 a-z]*\")", RegexOptions.IgnoreCase);
Match match = regEx.Match(inStr);
The match is not a success, let alone containing the 3 expected results 比赛未成功,更何况包含3个预期结果
However via http://regexhero.net the match is a success - I'm using regexhero as its SilverLight based so is using the .NET Regex engine... 但是,通过http://regexhero.net进行的匹配是成功的-我使用regexhero作为其SilverLight的基础,因此正在使用.NET Regex引擎...
Regexhero settings: 正则表达式设置:
Regular Expression 正则表达式
(\"[0-9 a-z]*\")
Target String 目标字符串
test "1" "b""3"
Result 结果
1: "1"
1: "b"
1: "3"
Can anyone explain whats wrong with my implementation? 谁能解释我的实现有什么问题?
I expect your regex is meant to be: 我希望您的正则表达式是:
Regex regEx = new Regex("(\"[0-9 a-z]*\")", RegexOptions.IgnoreCase);
or (exactly the same) 或(完全相同)
Regex regEx = new Regex(@"(""[0-9 a-z]*"")", RegexOptions.IgnoreCase);
It looks like this question might be a fruitful read for you: 看来这个问题可能对您有帮助:
What's the use/meaning of the @ character in variable names in C#? C#中变量名中@字符的用途/含义是什么?
You must change the regexp patern: 您必须更改正则表达式模式:
"@(\"[0-9 a-z]*\")"
to 至
@"(""[0-9 a-z]*"")"
Also You need change 你还需要改变
Match match = regEx.Match(inStr);
to 至
MatchCollection match = regEx.Matches(intstr);
Because Matches
searches an input string for all occurrences of a regular expression and returns all the matches but Match
searches the specified input string for the first occurrence of the regular expression 因为
Matches
在输入字符串中搜索所有出现的正则表达式并返回所有匹配项,但Match
在指定的输入字符串中搜索正则表达式的第一次出现
If You want result like this: 如果您想要这样的结果:
1: "1"
1: "b"
1: "3"
You need to change to Matches
您需要更改为
Matches
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.