新爸爸,所以我的眼睛很累,我想弄清楚为什么这个代码:
var regex = new Regex(@"(https:)?\/");
Console.WriteLine (regex.Replace("https://foo.com", ""));
发射:
foo.com
我只有一个正斜杠,为什么两个都被捕获到组中以进行替换?
最佳答案
In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.
每个 /
都匹配正则表达式模式 @"(https:)?\/"
。如果你尝试例如"https://foo/./com/"
,所有 /
都将被删除。
关于c# - 捕获的正则表达式超出预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31124044/