位置:首页 > 高级语言 > 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"