Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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的多个regex选项_C#_.net_Regex - Fatal编程技术网

带有C#regex的多个regex选项

带有C#regex的多个regex选项,c#,.net,regex,C#,.net,Regex,假设我有: Regex.Replace("aa cc bbbb","aa cc","",RegexOptions.IgnoreCase); 但我也需要忽略空白。因此,我找到了一个选项IgnorePatternWhitespace,但是如何向一个regex.Replace添加多个选项呢? 比如: Regex.Replace("aa cc bbbb", "aa cc", "", RegexOptions.IgnoreCase + RegexOptions.IgnorePatterWhit

假设我有:

Regex.Replace("aa cc bbbb","aa cc","",RegexOptions.IgnoreCase);
但我也需要忽略空白。因此,我找到了一个选项
IgnorePatternWhitespace
,但是如何向一个regex.Replace添加多个选项呢?
比如:

Regex.Replace("aa cc bbbb", "aa cc", "", 
    RegexOptions.IgnoreCase + RegexOptions.IgnorePatterWhitespace);
更新:
感谢您的回答,但此选项似乎不起作用:下面是一个测试示例:

Regex.Replace("aa cc bbbb", "aacc", "", 
    RegexOptions.IgnoreCase | RegexOptions.IgnorePatterWhitespace);
根据:

RegexOption枚举值的按位或组合

所以只要使用
OPT_A | OPT_B

Regex.Replace("aa cc bbbb","aa cc","",RegexOptions.IgnoreCase | RegexOptions.IgnorePatterWhitespace);
使用
|
操作符

编辑:

你完全错了
RegexOption.IgnorePatterWhitespace
忽略正则表达式中的空白,以便执行以下操作:

string pattern = @"
^                # Beginning of The Line
\d+              # Match one to n number but at least one..
";
然而,您认为输入空格会使
“aa cc bbbb”
变成
“aaccbbb”
,谢天谢地,这是错误的。

使用


您可以有任意多个regexoption,只需“或”带“|”的“regexoption”

例如

RegexOptions.Compiled | RegexOptions.IgnoreCase
忽略模式空白
从模式中消除未填充的空白,并启用注释 标有#。但是,IgnorePatternWhitespace值 影响或消除字符类中的空白。

因此:


“似乎不起作用”的信息量不大。你到底想要什么样的行为?作为一个问题,这根本没有意义——特别是当你看到了被接受的答案是什么,但甚至超出了这个范围!使用&而不是+,Regex.Replace(“aa cc bbb”,“aa cc”,“RegexOptions.IgnorePatternWhitespace&RegexOptions.IgnoreCase)vb.net version=Regex.Replace(“aa cc bbb”,“aa cc”,String.Empty,RegexOptions.IgnorePatternWhitespace和RegexOptions.IgnoreCase)
运算符工作正常:;)@user194076,如果您希望正则表达式“aacc”与“aa cc bbbb”中的“aa cc”匹配,那么您对regexoption的解释是错误的。您必须使用类似“aa\s?cc”的内容来匹配它,即使用\s?匹配可选空白。Regex.Replace(“aa cc bbbb”、“aacc”、“RegexOptions.IgnoreCase”|
RegexOptions.Compiled | RegexOptions.IgnoreCase
string result = Regex.Replace("aa cc bbbb","aa|cc","",RegexOptions.IgnoreCase).Trim();