为什么正则表达式替换方法不起作用?

时间:2022-09-12 16:52:43

In the following code I want to replace every occurrence of "U.S.A" with "united states of America" and every occurrence of "uk" with "united kingdom" in a string, but it does not seem to work. How do I fix it?

在下面的代码中,我想用“美国美国”取代每一次出现的“U.S.A”,并用字符串中的“联合王国”每次出现“uk”,但它似乎不起作用。我如何解决它?

class Program
    {
        static void Main(string[] args)
        {
            string s = "the U.S.A love UK";
            Console.WriteLine(replace(s));
        }

        public static string replace(string s)
        {
            s = Regex.Replace(s, @"^U.S.A", " United state Of America");
            s = Regex.Replace(s, @"^Uk", "United kingdom");
            return s;
        }

    }

5 个解决方案

#1


For simple replacements, you don't need Regular expressions:

对于简单替换,您不需要正则表达式:

string s = "the U.S.A love UK";
s = s.Replace("U.S.A", "United States of America").Replace("UK", "United Kingdom");

#2


Well, look at the search pattern in your regex.

那么,看看你的正则表达式中的搜索模式。

The ^ has a specific meaning. (as do the . but they won't actually fail in this case, but they aren't doing what you think they are)

^具有特定含义。 (就像那样。但是在这种情况下他们实际上不会失败,但是他们没有做你认为的那样)

#3


  1. The . in your U.S.A expression will match any single character. I doubt that's what you intend.
  2. 这个。在您的U.S.A表达式中将匹配任何单个字符。我怀疑这是你的意图。

  3. The ^ pins your expression to the beginning of the string. You don't want it.
  4. ^将表达式固定到字符串的开头。你不想要它。

  5. By default, regular expressions are case sensitive. If you want Uk to match UK you need to specify a case-insensitive compare.
  6. 默认情况下,正则表达式区分大小写。如果您希望Uk与UK匹配,则需要指定不区分大小写的比较。

#4


To expand upon annakata's answer:

扩展annakata的答案:

In regular expressions the '^' character means "match at the beginning of the String", so if you wanted "U.S.A." to be replaced, it would have to be the first six characters in the string, same for UK. Drop the '^' from your RE, and it should work.

在正则表达式中,'^'字符表示“在字符串的开头匹配”,所以如果你想要“U.S.A”。要被替换,它必须是字符串中的前六个字符,对于UK来说是相同的。从你的RE中删除'^',它应该可以工作。

And regarding the comment about the '.' character, in regular expressions this means match any character. If you wish to match a literal '.' you must escape it like this "."

关于''的评论。在正则表达式中,这意味着匹配任何字符。如果你想匹配文字'。'你必须像这样逃避它。“

#5


There are already enough correct answers above. So I'll just add that an excellent resource for better understanding regular expressions is Mastering Regular Expressions

上面已经有足够的正确答案了。所以我要补充说,为了更好地理解正则表达式,一个很好的资源是掌握正则表达式

#1


For simple replacements, you don't need Regular expressions:

对于简单替换,您不需要正则表达式:

string s = "the U.S.A love UK";
s = s.Replace("U.S.A", "United States of America").Replace("UK", "United Kingdom");

#2


Well, look at the search pattern in your regex.

那么,看看你的正则表达式中的搜索模式。

The ^ has a specific meaning. (as do the . but they won't actually fail in this case, but they aren't doing what you think they are)

^具有特定含义。 (就像那样。但是在这种情况下他们实际上不会失败,但是他们没有做你认为的那样)

#3


  1. The . in your U.S.A expression will match any single character. I doubt that's what you intend.
  2. 这个。在您的U.S.A表达式中将匹配任何单个字符。我怀疑这是你的意图。

  3. The ^ pins your expression to the beginning of the string. You don't want it.
  4. ^将表达式固定到字符串的开头。你不想要它。

  5. By default, regular expressions are case sensitive. If you want Uk to match UK you need to specify a case-insensitive compare.
  6. 默认情况下,正则表达式区分大小写。如果您希望Uk与UK匹配,则需要指定不区分大小写的比较。

#4


To expand upon annakata's answer:

扩展annakata的答案:

In regular expressions the '^' character means "match at the beginning of the String", so if you wanted "U.S.A." to be replaced, it would have to be the first six characters in the string, same for UK. Drop the '^' from your RE, and it should work.

在正则表达式中,'^'字符表示“在字符串的开头匹配”,所以如果你想要“U.S.A”。要被替换,它必须是字符串中的前六个字符,对于UK来说是相同的。从你的RE中删除'^',它应该可以工作。

And regarding the comment about the '.' character, in regular expressions this means match any character. If you wish to match a literal '.' you must escape it like this "."

关于''的评论。在正则表达式中,这意味着匹配任何字符。如果你想匹配文字'。'你必须像这样逃避它。“

#5


There are already enough correct answers above. So I'll just add that an excellent resource for better understanding regular expressions is Mastering Regular Expressions

上面已经有足够的正确答案了。所以我要补充说,为了更好地理解正则表达式,一个很好的资源是掌握正则表达式