0

私は正規表現に関してはまったくの初心者であり、誰かが私を助けてくれるかどうか疑問に思っていました. ここで正規表現を使用することが正しいアプローチであるかどうかはわかりません。より良いアイデアがあれば、遠慮なくご連絡ください。(多くの文字列をループします)。

基本的に、文字列を検索/置換し、一致を {} でラップし、文字列の元のケースを維持したいと思います。

例:

Source: "The CAT sat on the mat."    
Find/Replace: "cat"    
Result: "The {CAT} sat on the mat."

検索/置換が最初の出現でのみ機能することを望みます。また、検索/置換が実際に一致したかどうかも知る必要があります。

物事を十分に明確に説明できたことを願っています。

ありがとうございました。

4

3 に答える 3

5
Regex theRegex = 
    new Regex("(" + Regex.Escape(FindReplace) + ")", RegexOptions.IgnoreCase);
theRegex.Replace(Source, "{$1}", 1);

単語境界の許容範囲が必要な場合:

 Regex theRegex = 
     (@"([\W_])(" + Regex.Escape(FindReplace) + @")([\W_])", RegexOptions.IgnoreCase)
 theRegex.Replace(str, "$1{$2}$3", 1)
于 2013-08-05T08:29:57.510 に答える
1

多くの文字列をループする場合、おそらく Regex は最良のアイデアではない可能性があります。これは優れたツールですが、最速ではありません。

これも機能するサンプルコードです。

        var str = "The Cat ate a mouse";
        var search = "cat";
        var index = str.IndexOf(search, StringComparison.CurrentCultureIgnoreCase);
        if (index == -1)
          throw new Exception("String not found"); //or do something else in this case here
        var newStr = str.Substring(0, index) + "{" + str.Substring(index, search.Length) + "}" + str.Substring(index + search.Length);

編集:

コメントで指摘されているように、上記のコードにはいくつかの問題があります。

そこで、正規表現を使用せずに機能させる方法を見つけることにしました。誤解しないでほしいのですが、私は次の男と同じくらい正規表現が大好きです。私は主に好奇心からこれを行いました。;)

これが私が出会ったものです:

public static class StringExtendsionsMethods
{
    public static int IndexOfUsingBoundary(this String s, String word)
    {
        var firstLetter = word[0].ToString();
        StringBuilder sb = new StringBuilder();
        bool previousWasLetterOrDigit = false;
        int i = 0;
        while (i < s.Length - word.Length + 1)
        {
            bool wordFound = false;
            char c = s[i];

            if (c.ToString().Equals(firstLetter, StringComparison.CurrentCultureIgnoreCase))
                if (!previousWasLetterOrDigit)
                    if (s.Substring(i, word.Length).Equals(word, StringComparison.CurrentCultureIgnoreCase))
                    {
                        wordFound = true;
                        bool wholeWordFound = true;
                        if (s.Length > i + word.Length)
                        {
                            if (Char.IsLetterOrDigit(s[i + word.Length]))
                                wholeWordFound = false;
                        }

                        if (wholeWordFound)
                            return i;

                        sb.Append(word);

                        i += word.Length;
                    }

            if (!wordFound)
            {
                previousWasLetterOrDigit = Char.IsLetterOrDigit(c);
                sb.Append(c);
                i++;
            }
        }

        return -1;
    }
}

しかし、私はこれを手柄にすることはできません!ここで、StackOverflowでいくつかのグーグルを行った後、これを見つけて変更しました。;)

IndexOf上記のコードの標準の代わりに、このメソッドを使用してください。

于 2013-08-05T08:33:06.897 に答える
1

これを試して:

class Program
{
    const string FindReplace = "cat";
    static void Main(string[] args)
    {
        var input = "The CAT sat on the mat as a cat.";
        var result = Regex
            .Replace(
            input,
            "(?<=.*)" + FindReplace + "(?=.*)",
            m =>
            {
                return "{" + m.Value.ToUpper() + "}";
            },
            RegexOptions.IgnoreCase);
        Console.WriteLine(result);
    }
}
于 2013-08-05T09:29:02.333 に答える