我想在字符串中找到所有匹配的“\ abc / something / \ abc /”忽略某些部分:
如果我的字符串是:
string str = @"\abc/something\abc/\abc/something\abc/;
我应该如何编写模式以便获得两个匹配的\ abc / something \ abc /?
由于在这种情况下使用^和$来定义开头和结尾不会起作用,因为整个字符串的开头和结尾都是这样。
答案 0 :(得分:0)
您可以在此处试用各种正则表达式:http://regexstorm.net/tester
但看起来你应该匹配=IF(A3="tf06",F3,"")
答案 1 :(得分:0)
CaptureCollection应该获得一组中的所有匹配,
一次通过。
此(?:.*?(\\\w+/))+
或此(?:(\\\w+/)|.)+
举个例子。
(?:
.*?
( \\ \w+ / ) # (1)
)+
C#
string str = @"\\adbc/\\abrc/";
//Regex RxAbc = new Regex(@"(?:(\\\w+/)|.)+");
Regex RxAbc = new Regex(@"(?:.*?(\\\w+/))+");
Match _mAbc = RxAbc.Match(str);
if (_mAbc.Success)
{
CaptureCollection cc = _mAbc.Groups[1].Captures;
for (int i = 0; i < cc.Count; i++)
{
Console.WriteLine("{0}", cc[i].Value);
}
}
输出:
\adbc/
\abrc/
编辑 - 来自评论another example that is like this: @"\abc/something\abc/\abc/somethingELSE\abc/, and return 2 matches of \abc/something\abc/
# ?:.*?((\\\w+/).*?\2))+
(?:
.*?
( # (1 start)
( \\ \w+ / ) # (2)
.*?
\2
) # (1 end)
)+
C#
string str = @"\abcX/somethinghere\abcX/\abcY/there\abcY/\abcZ/\abcZ/";
Regex RxAbc = new Regex(@"(?:.*?((\\\w+/).*?\2))+");
Match _mAbc = RxAbc.Match(str);
if (_mAbc.Success)
{
CaptureCollection ccAbcToAbc = _mAbc.Groups[1].Captures;
CaptureCollection ccAbc = _mAbc.Groups[2].Captures;
for (int i = 0; i < ccAbcToAbc.Count; i++)
{
Console.WriteLine("Found keyword {0}, in string {1}", ccAbc[i].Value, ccAbcToAbc[i].Value);
}
Console.WriteLine("------------------------\n");
}
输出:
Found keyword \abcX/, in string \abcX/somethinghere\abcX/
Found keyword \abcY/, in string \abcY/there\abcY/
Found keyword \abcZ/, in string \abcZ/\abcZ/
相同的代码不同的输入
输入= @"\abc/somethinghere\abc/\abc/there\abc/\abc/\abc/"
输出:
Found keyword \abc/, in string \abc/somethinghere\abc/
Found keyword \abc/, in string \abc/there\abc/
Found keyword \abc/, in string \abc/\abc/