1

「let's」という単語を除いて、テキストからすべての「's」を削除する必要があります。

例:「ジェリーレッツケーキ」>>「ジェリーレッツケーキ」。

strText = Regex.Replace(strText, @"\b(?!let's)([a-z-]+)'s\b", @"$1");

-これは機能しますが、大きなテキストでは時間がかかります。

strText = Regex.Replace(strText, @"\b(?!let's)(?<=[a-z-]+)'s\b", "");
  • これは「レッツ」を無視していません

2番目のステートメントで何が間違っていますか?

4

4 に答える 4

3

これが実際に必要なものである場合は、正規表現を使用せずに次のようなことを行うのが賢明です。

strText = strText.Replace(" let's ", " let''s ")
                 .Replace("'s", "")
                 .Replace(" let'", " let's");
于 2012-07-10T16:37:14.563 に答える
1

また、あなたはあなた自身のコードを書くかもしれないと思います。

private string myReplace(string text)
    {
        if (!text.Contains(' '))
        {
            return text.ToLower().Equals("let's") ? text : text.Replace("'s", string.Empty);
        }
        else
        {
            int index = text.IndexOf(' ');
            return myReplace(text.Substring(0, index)) + " " + myReplace(text.Substring(index + 1));
        }
    }
于 2012-07-10T17:18:07.513 に答える
1

あなたが逃した単純なトリックは\b、ネガティブルックビハインドで使用することです:

(?<!\blet)'s

実例: http: //regexr.com?31g9c

于 2012-07-10T17:02:21.820 に答える
0

正直なところ、この場合は、昔ながらの方法で弦を歩き、「レッツ」という言葉のどこにいるかを追跡し、それを特に処理するのがおそらく最善です。

コード(テストされていませんが、うまくいけばあなたはアイデアを得るでしょう):

StringBuilder stripped = new StringBuilder();
int letsPos = -1;

for(int i = 0; i < strText.Length; i++)
{
     char thisChar = strText[i];

     switch(letsPos)
     {    
         case -1:
             if(i == 0 || thisChar == ' ')
                 letsPos = 0;
             break;
         case 0:
             letsPos = (thisChar == 'l') ? 1 : -1;
             break;
         case 1:
             letsPos = (thisChar == 'e') ? 2 : -1;
             break;
         case 2:
             letsPos = (thisChar == 't') ? 3 : -1;
             break;
         case 3:
             letsPos = (thisChar == '\'') ? 4 : -1;
             break;
         case 4:
             letsPos = (thisChar == 's') ? 5 : -1;
             break;
         case 5:
             letsPos = (thisChar == ' ') ? 0 : -1;
             break;
     }

     if(thisChar == '\'' && 
         i+1 < strText.Length && 
         strText[i+1] == 's' && 
         (letsPos < 4 || (i+2 < strText.Length && strText[i+2] != ' '))
     {
         //skip the "'s"
         i += 2;
         letsPos = -1;
     }
     else
     {
         //not "'s" or we have a whole-word "let's"
         stripped.Append(thisChar);
     }
}

return stripped.ToString();
于 2012-07-10T16:56:47.797 に答える