Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
搜索内部模式的C#RegExp_C#_Regex_Text - Fatal编程技术网

搜索内部模式的C#RegExp

搜索内部模式的C#RegExp,c#,regex,text,C#,Regex,Text,我有这样一个字符串: [[[a]]][[[b]]][[[c]]] 我想提取这些: a b c 所以我写了以下模式: @"\[\[\[(.+?)\]\]\]" 使用以下代码 string input = "[[[a]]][[[b]]][[[c]]]"; Regex regexObj = new Regex(@"\[\[\[(.+?)\]\]\]"); foreach (Match er in regexObj.Matches(input)) { MessageBox.Show(e

我有这样一个字符串:

[[[a]]][[[b]]][[[c]]]
我想提取这些:

a
b
c
所以我写了以下模式:

@"\[\[\[(.+?)\]\]\]"
使用以下代码

string input = "[[[a]]][[[b]]][[[c]]]";
Regex regexObj = new Regex(@"\[\[\[(.+?)\]\]\]");
foreach (Match er in regexObj.Matches(input)) 
{ 
    MessageBox.Show(er.Value); 
} 
结果是:

[[[a]]]
[[[b]]]
[[[c]]]

怎么了?你能帮我吗?

而不是
er.Value
你需要使用
er.Groups[1].Value

er.Value
er.Groups[0].Value
相同,它包含一个与整个正则表达式模式匹配的字符串。从索引1向上的每个后续元素表示一个捕获的组

请参阅以供参考。

为什么不使用此模式(它为您的示例提供了正确的输出)

试试这个:

[^\p{Ps}\p{Pe}]

这使用unicode的开始和结束括号。

string input=“[[a]]][[b]][[c]]”;Regex regexObj=新的Regex(@“[[(.+?)]]]”);foreach(regexObj.Matches(input)){MessageBox.Show(er.Value);}您正在获取匹配项,但没有查找组的捕获:er.groups[0]。捕获[0]。Value。@Boggin
groups[0]
是全部匹配项。在我吃早餐的时候,这是一个很简单的问题吗?不公平。:)如果真正的字符串(问题没有简化)不仅包含a-z,而且OP需要在方括号内精确提取值,该怎么办?
[^\p{Ps}\p{Pe}]