Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
如何使用reg ex计算字符串(.NET Framework)中子字符串的出现次数_.net_Regex_String - Fatal编程技术网

如何使用reg ex计算字符串(.NET Framework)中子字符串的出现次数

如何使用reg ex计算字符串(.NET Framework)中子字符串的出现次数,.net,regex,string,.net,Regex,String,如果字符串包含以下内容: 如何创建返回值为2的正则表达式。这意味着,由于https://在该字符串中出现两次,因此它应该返回2 我目前正在使用这个正则表达式在两个https://之间进行解析,但不知道如何调整它以在本例中返回字符串2中https//的数目 (?s)(?<=https://).+?(?=https://) 使用.NET框架。非常感谢您的帮助这里有一个棘手的非正则表达式方法: var inp = "https://website1.comhttps://website2.co

如果字符串包含以下内容:

如何创建返回值为2的正则表达式。这意味着,由于https://在该字符串中出现两次,因此它应该返回2

我目前正在使用这个正则表达式在两个https://之间进行解析,但不知道如何调整它以在本例中返回字符串2中https//的数目

(?s)(?<=https://).+?(?=https://)

使用.NET框架。非常感谢您的帮助

这里有一个棘手的非正则表达式方法:

var inp = "https://website1.comhttps://website2.com";
var cnt_nonrgx = inp.Length - inp.Replace("https://", "https:/").Length;
var cnt_rgx = Regex.Matches(inp, "https://").Count;
以下是正则表达式的方法:

var inp = "https://website1.comhttps://website2.com";
var cnt_nonrgx = inp.Length - inp.Replace("https://", "https:/").Length;
var cnt_rgx = Regex.Matches(inp, "https://").Count;
还有一个经典的方式:

var cnt_liof = 0;
var idx = inp.IndexOf("https://");
while (idx > -1)
{
    cnt_liof += 1;
    idx = inp.IndexOf("https://", idx + 1);
}

所有结果都是2。

使用此正则表达式https:\/\/然后计算匹配的结果。完美!非常简单,谢谢。我想您根本不需要正则表达式,只需计算子字符串https://。。。