简体   繁体   English

我可以在 C# 中将正则表达式与 String.Replace 一起使用吗?

[英]Can I use regular expressions with String.Replace in C#?

For example I have code below string txt="I have strings like West, and West; and west, and Western."例如,我在字符串 txt 下有代码=“我有像 West 和 West 这样的字符串;还有 west 和 Western。”

I would like to replace the word west or West with some other word.我想用其他词代替西或西这个词。 But I would like not to replace West in Western.但我不想在西方取代西方。

  1. Can I use regular expression in string.replace?我可以在 string.replace 中使用正则表达式吗? I used inputText.Replace("(\\\\sWest.\\\\s)",temp);我使用inputText.Replace("(\\\\sWest.\\\\s)",temp); It dos not work.它不起作用。

To replace the whole word (rather than part of the word):替换整个单词(而不是单词的一部分):

string s = "Go west Life is peaceful there";
s = Regex.Replace(s, @"\bwest\b", "something");

Answer to the question is NO - you cannot use regexp in string.Replace.问题的答案是否定的- 您不能在 string.Replace 中使用正则表达式。

If you want to use a regular expression, you must use the Regex class, as everyone stated in their answers.如果你想使用正则表达式,你必须使用 Regex 类,正如每个人在他们的答案中所说的那样。

Have you looked at Regex.Replace ?你看过Regex.Replace吗? Also, be sure to catch the return value;另外,一定要抓住返回值; Replace (via any string mechanism) returns a new string - it doesn't do an in-place replace. Replace (通过任何字符串机制)返回一个字符串 - 它不会进行就地替换。

Try using the System.Text.RegularExpressions.Regex class.尝试使用System.Text.RegularExpressions.Regex类。 It has a static Replace method.它有一个静态的Replace方法。 I'm not good with regular expressions, but something like我不擅长正则表达式,但像

string outputText = Regex.Replace(inputText, "(\\sWest.\\s)", temp);

should work, if your regular expression is correct.如果您的正则表达式正确,应该可以工作。

Insert the regular expression in the code before class在类之前的代码中插入正则表达式

using System.Text.RegularExpressions;

below is the code for string replace using regex下面是使用正则表达式替换字符串的代码

string input = "Dot > Not Perls";
// Use Regex.Replace to replace the pattern in the input.
string output = Regex.Replace(input, "some string", ">");

source : http://www.dotnetperls.com/regex-replace来源: http : //www.dotnetperls.com/regex-replace

USe this code if you want it to be case insensitive如果您希望它不区分大小写,请使用此代码

string pattern = @"\bwest\b";
string modifiedString = Regex.Replace(input, pattern, strReplacement, RegexOptions.IgnoreCase);

In Java, String#replace accepts strings in regex format but C# can do this as well using extensions:在 Java 中, String#replace接受 regex 格式的字符串,但 C# 也可以使用扩展来做到这一点:

public static string ReplaceX(this string text, string regex, string replacement) {
    return Regex.Replace(text, regex, replacement);
}

And use it like:并像这样使用它:

var text = "      space          more spaces  ";
text.Trim().ReplaceX(@"\s+", " "); // "space more spaces"

I agree with Robert Harvey's solution except for one small modification:我同意罗伯特哈维的解决方案,除了一个小的修改:

s = Regex.Replace(s, @"\bwest\b", "something", RegexOptions.IgnoreCase);

This will replace both "West" and "west" with your new word这将用您的新词替换“West”和“west”

class Regex is static, when You use the method Replace (Regex.Replace).当您使用方法 Replace (Regex.Replace) 时,类 Regex 是静态的。

public static class Extension
{
        public static string ReplaceValue(string data,string criteria)
        {
              return s = Regex.Replace(s, @"\bwest\b", "something");
         }
}

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

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