Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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 - Fatal编程技术网

C# 循环直到字符串C结束

C# 循环直到字符串C结束,c#,regex,C#,Regex,我有一个字符串输入是由用户执行的,它有一个表达式要根据正则表达式模式匹配器进行检查 我希望在字符串中循环直到EOF。我在考虑使用input.Length,但我不知道如何继续比较数字。如果整个字符串与模式一致,则返回TRUE,否则返回FALSE。这是我到现在为止到达的地方 private void checkInput (String input) { { String acceptedInput = "(?=\\()|(?<=\\)\\d)"; /

我有一个字符串输入是由用户执行的,它有一个表达式要根据正则表达式模式匹配器进行检查

我希望在字符串中循环直到EOF。我在考虑使用input.Length,但我不知道如何继续比较数字。如果整个字符串与模式一致,则返回TRUE,否则返回FALSE。这是我到现在为止到达的地方

private void checkInput (String input)
{
    {
        String acceptedInput = "(?=\\()|(?<=\\)\\d)";

        // Need a loop until End of String

        // (while ?)
        {
            foreach (Match match in Regex.Matches(input, acceptedInput))
            {
                outputDialog.AppendText("Correct");
            }

            return true;

        }

        return false;
    }
}
请问有什么办法吗


感谢您在字符串中循环每个字符:

for (int i = 0; i < stringVariable.Length; i++)
{
    char x = stringVariable[i]; //is the i'th character of the string
}
但是,如果您使用正则表达式,这种方法就没有意义了,正则表达式通常适用于整个字符串

也许可以解释一下你想要达到什么目的?

使用

i、 e


您不需要循环:

string toAvoid = "$%&#";
if (input.IndexOfAny(toAvoid.ToCharArray()) != -1)
{
  // the input contains forbidden characters
}

我希望遍历字符串,或者您需要遍历每个字符?我最需要的是检查整个字符串,就像在另一个方法中一样,我需要检查bool返回是否为真,以查看是否继续或显示errorStringvariable是字符串变量:在我的情况下输入。感谢Cjb110像charmcjb110一样工作,checkInput是一种检查模式是否良好的方法。然后返回TRUE或FALSE。我在程序中将public void编辑为public bool。如果它返回true,那么它可以继续它正在做的事情。否则会停止程序为了完成此操作,ToCharray创建了一个副本,如果您只是执行读取操作,则不太可能需要该副本。@cjb110:不知道我可以直接访问字符串[index]:
string toAvoid = "$%&#";
if (input.IndexOfAny(toAvoid.ToCharArray()) != -1)
{
  // the input contains forbidden characters
}