简体   繁体   English

在regex.replace中忽略区分大小写?

[英]ignore case sensitive in regex.replace?

I have this code to search in a string and replace some text with other text: 我有这个代码在字符串中搜索并用其他文本替换一些文本:

Regex regexText = new Regex(textToReplace);
retval = regexText.Replace(retval, Newtext);

textToReplace may be "welcome" or "client" or anything. textToReplace可以是“welcome”或“client”或任何东西。

I want to ignore case for textToReplace so that "welcome" and "Welcome" both match. 我想忽略textToReplace大小写,以便“welcome”和“Welcome”都匹配。

How can I do this? 我怎样才能做到这一点?

你可以尝试:

Regex regexText = new Regex(textToReplace, RegexOptions.IgnoreCase);

You simply pass the option RegexOptions.IgnoreCase like so: 您只需传递RegexOptions.IgnoreCase选项, RegexOptions.IgnoreCase所示:

Regex regexText = new Regex(textToReplace, RegexOptions.IgnoreCase);
retval = regexText.Replace(retval, Newtext);

Or, if you prefer, you can pass the option directly to the Replace method : 或者,如果您愿意,可以将选项直接传递给Replace方法

retval = Regex.Replace(retval, textToReplace, Newtext, RegexOptions.IgnoreCase);

A list of the available options you can set for regexes is available at the RegexOptions documentation page . 可以在RegexOptions文档页面上找到可以为regex设置的可用选项列表。

There's a Regex.Replace overload with RegexOptions . RegexOptions有一个Regex.Replace 重载 Those options include an IgnoreCase value. 这些选项包括IgnoreCase值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM