Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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#Regex:字符串出现一次或没有出现_C#_Regex - Fatal编程技术网

C#Regex:字符串出现一次或没有出现

C#Regex:字符串出现一次或没有出现,c#,regex,C#,Regex,有没有更好的方法来查找C#中字符串的一次出现或不出现?现在,我使用以下语句来实现同样的目标 Regex.Replace(input, @"[0-9]+([s][t][r][i][n][g])", "$1", RegexOptions.IgnoreCase); 使用string.contains方法: string s1 = "The quick brown fox jumps over the lazy dog"; string s2 = "fox"; if (s1.contains(s2

有没有更好的方法来查找C#中字符串的一次出现或不出现?现在,我使用以下语句来实现同样的目标

Regex.Replace(input, @"[0-9]+([s][t][r][i][n][g])", "$1", RegexOptions.IgnoreCase);

使用string.contains方法:

string s1 = "The quick brown fox jumps over the lazy dog";
string s2 = "fox"; 

if (s1.contains(s2))
{
Regex.Replace(input, @"[0-9]+([s][t][r][i][n][g])", "$1", RegexOptions.IgnoreCase);
}

修饰符告诉您只需要一次或不需要发生。如果我没弄错的话,你想要

Regex.Replace(input, @"[0-9]+((?:string)?)", "$1", RegexOptions.IgnoreCase);
?:
在字符串代表非捕获组之前,因此内部大括号不被视为一个组,并且不能通过替换中的
.Groups
$
表达式访问


见重复部分

正则表达式似乎正在检查一个或多个后跟
字符串的数字,这不是您在说明中所述的。你到底想匹配什么-请提供输入和预期结果的示例。