簡體   English   中英

請幫助我創建正則表達式

[英]Please Help me in creating regular expression

請告訴我從字符串中替換值“ _ $$ 12”的正則表達式是什么,其中“ 12”可以是任何數字。 我嘗試了各種組合,但是'$'造成了問題。

由於$在正則表達式中具有特殊含義,因此您需要對其進行轉義:

@"_\$\$\d\d"

$正則表達式中有特殊含義,它標志着字符串的結尾。 例如

Regex.Replace(input_string,@"_\$\$\(d+)", @"\1");

將僅用12替換_$$12 12

var sanitized = Regex.Replace("_$$12", @"_\$\$[0-9]+", "ReplacementString");

檢查一下:

using System.Text.RegularExpressions;

class RegExSample 
{
  static void Main() 
  {
    string text = "text _$$12 text";
    string result = Regex.Replace(text, @"_\$\$\d+", "#replacement#");
    System.Console.WriteLine("result = [" + result + "]");
  }
}

請參閱此處的代碼。

嘗試這個。

        string input = "_$$12";
        string output = Regex.Replace(input, @"_\$\$", string.Empty);

輸出將為12。如果您增加輸入,例如“ _ $$ 123456”,則輸出將為123456。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM