C# 正则表达式,在其他组之间替换组?

C# 正则表达式,在其他组之间替换组?,c#,regex,C#,Regex,我有这样一个正则表达式: string ipPort = @"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[\s\S]*?[0-9]{1,5}"; Regex Rx = new Regex(ipPort,RegexOptions.Singleline); List<string> catched = new List<string>(); foreach (Match ItemMatch in Rx.Matches(pag

我有这样一个正则表达式:

string ipPort = @"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}[\s\S]*?[0-9]{1,5}";
Regex Rx = new Regex(ipPort,RegexOptions.Singleline);
List<string> catched = new List<string>();

foreach (Match ItemMatch in Rx.Matches(page))
{
    catched.Add(ItemMatch.ToString());
}
string ipPort=@“[0-9]{1,3}\[0-9]{1,3}\[0-9]{1,3}\[0-9]{1,3}[\s\s]*?[0-9]{1,5}”;
Regex Rx=新的Regex(ipPort,RegexOptions.Singleline);
捕获列表=新列表();
foreach(Rx.Matches中的Match ItemMatch(第页))
{
catched.Add(itemmach.ToString());
}

它将查找ip,后跟任意数量的字符,后跟端口号。我希望这个“任意数量的字符”替换为单个冒号“:”。如何做到这一点,我对正则表达式不是很有经验…

您可以使用这个使用lookarounds的通用表达式来查找前缀和后缀之间的模式:

(?<=prefix)find(?=suffix)
您还可以将
[\s\s]
(空格或非空格字符)替换为
(任何字符)

应用于我们的一般表达式,现在我们有:

前缀(ip):
([0-9]{1,3}\){3}[0-9]{1,3}

查找(要用冒号替换的内容):
[^0-9].*?


后缀(端口):
[0-9]{1,5}

您可以使用这个使用lookarounds的通用表达式来查找前缀和后缀之间的模式:

(?<=prefix)find(?=suffix)
您还可以将
[\s\s]
(空格或非空格字符)替换为
(任何字符)

应用于我们的一般表达式,现在我们有:

前缀(ip):
([0-9]{1,3}\){3}[0-9]{1,3}

查找(要用冒号替换的内容):
[^0-9].*?

后缀(端口):
[0-9]{1,5}

(?<=([0-9]{1,3}\.){3}[0-9]{1,3})[^0-9].*?(?=[0-9]{1,5})