位置:首頁 > 高級語言 > C#教學 > C#替換正則表達式

C#替換正則表達式

替換被用於替換模式。下表列出了取代常見類型:

字符 描述 模式 替換模式 輸入字符串 輸出字符串
$number 替換匹配的組號子字符串 (w+)(s)(w+) $3$2$1 "one two" "two one"
${name} 替代匹配命名子字符串groupname. (?< word1>w+)(s)(?< word2>w+) ${word2} ${word1} "one two" "two one"
$$ 替代文字 "$". (d+)s?USD $$$1 "103 USD" "$103"
$& 替代了整體匹配的一個副本 ($*(d*(.+d+)?){1}) **$& "$1.30" "**$1.30**"
$` Substitutes all the text of the input string before the match. B+ $` "AABBCC" "AAAACC"
$' Substitutes all the text of the input string after the match. B+ $' "AABBCC" "AACCCC"
$+ Substitutes the last group that was captured. B+(C+) $+ "AABBCCDD" AACCDD
$_ Substitutes the entire input string. B+ $_ "AABBCC" "AAAABBCCCC"