Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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
C# 计算与字符串中的模式匹配的令牌数_C#_C++_Regex_Pattern Matching - Fatal编程技术网

C# 计算与字符串中的模式匹配的令牌数

C# 计算与字符串中的模式匹配的令牌数,c#,c++,regex,pattern-matching,C#,C++,Regex,Pattern Matching,一种计算与字符串中的模式匹配的令牌数的方法 令牌是一个“$”后跟“$$”,在“$”和“$$”之间可以有任意数量的字符 例如:“$123$$,$ab$$,$qqwe123$$ 输入字符串可以是“$122$$dddd$1aasds$$” 对于上述字符串,方法的输出应为2 程序语言可以是C或C++。 以下是我提出的代码,但我正试图找到最好的方法: static int CalculateTokenCount() { string s = "$ab$$ask$$$

一种计算与字符串中的模式匹配的令牌数的方法

令牌是一个“$”后跟“$$”,在“$”和“$$”之间可以有任意数量的字符

例如:
“$123$$,$ab$$,$qqwe123$$

输入字符串可以是
“$122$$dddd$1aasds$$”

对于上述字符串,方法的输出应为2

程序语言可以是C或C++。 以下是我提出的代码,但我正试图找到最好的方法:

static int CalculateTokenCount()
        {
            string s = "$ab$$ask$$$$123$$";
            int tokenCount = 0;
            bool foundOneDollar = false;
            bool foundSecondDollar = false;

            if (string.IsNullOrEmpty(s))
            {
                return tokenCount;
            }
            for (int i = 0, x = 0; i < s.Length; i++)
            {
                if (s[i] == '$' && !foundOneDollar)
                {
                    foundOneDollar = true;
                    continue;
                }

                if (foundOneDollar)
                {
                    if (s[i] == '$' && !foundSecondDollar)
                    {
                        foundSecondDollar = true;
                        continue;
                    }
                }

                if (foundSecondDollar)
                {
                    if (s[i] == '$')
                    {
                        tokenCount++;
                    }  
                    foundSecondDollar = false;
                }
            }
            Console.WriteLine(tokenCount);
            return tokenCount;
        }
static int CalculateTokenCount()
{
字符串s=“$ab$$ask$$$$$123$$”;
int-tokenCount=0;
bool foundOneDollar=false;
bool foundSecondDollar=false;
if(string.IsNullOrEmpty)
{
返回令牌计数;
}
对于(int i=0,x=0;i
看看如何使用

在指定的输入字符串中搜索正则表达式的所有匹配项 表情


也可以看看

看看如何使用

在指定的输入字符串中搜索正则表达式的所有匹配项 表情


还可以查看一下

您可以使用以下正则表达式

\$.*?\$\$
这会检测到任意数量的字符,甚至零个字符。如果需要至少一个字符,请将
*
替换为
+

正如@astander已经说过的,要检索匹配的计数,请使用
Regex.matches

string input = "$122$$dddd$1aasds$$";
string pattern = @"\$.*?\$\$";
Regex rgx = new Regex(pattern);
MatchCollection matches = rgx.Matches(input);
int count = matches.Count();

可以使用以下正则表达式

\$.*?\$\$
这会检测到任意数量的字符,甚至零个字符。如果需要至少一个字符,请将
*
替换为
+

正如@astander已经说过的,要检索匹配的计数,请使用
Regex.matches

string input = "$122$$dddd$1aasds$$";
string pattern = @"\$.*?\$\$";
Regex rgx = new Regex(pattern);
MatchCollection matches = rgx.Matches(input);
int count = matches.Count();

在我看来,这是一个更好的评论。在我看来,这是一个更好的评论。很好的问题。你有我去解决它的权利。你有我去解决它的权利