c# - 捕获的正则表达式超出预期

标签 c# regex

新爸爸,所以我的眼睛很累,我想弄清楚为什么这个代码:

var regex = new Regex(@"(https:)?\/");
Console.WriteLine (regex.Replace("https://foo.com", ""));

发射:

foo.com

我只有一个正斜杠,为什么两个都被捕获到组中以进行替换?

最佳答案

Regex.Replace :

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/

相关文章:

regex - 提取以数字开头和结尾的子字符串

c# - 如何在 NHibernate 中强制加载代理对象?

c# - string[][] 变为小写

c# - 如何在 Linux 上的 .net Core 中获取系统规范

regex - 用 sed 替换第 n 个表达式

python - PySpark textFile 命令中的正则表达式

c# - string message = Regex.Replace(message, "\\", "") 有问题;

c#:脉冲并等待

c# - 当 WPF 应用程序最大化时,UI 经常挂起

regex - U-Boot版本提取对比