替换被用在替换模式中。如下表所示:
字符 | 描述 | 模式 | 替换模式 | 输入字符 | 结果字符 |
---|---|---|---|---|---|
$number | 用被命名的数字组替换匹配的字符串。 | \b(\w+)(\s)(\w+)\b | $3$2$1 | "one two" | "two one" |
${name} | 用被命名的 groupname替换匹配的字符串。 | \b(?< word1>\w+)(\s)(?< word2>\w+)\b | ${word2} ${word1} | "one two" | "two one" |
$$ | 替换美元符"$" | \b(\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" |
转载本站内容时,请务必注明来自W3xue,违者必究。