Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Giải thích Substitution (Thay thế) trong Regular Expression C#

Regular expression là pattern match với input text. Substitutions dùng trong replacement patterns.

CharacterMô tảPatternReplacementInputResult
$numberThay substring match bởi group number\b(\w+)(\s)(\w+)\b$3$2$1"one two""two one"
${name}Thay substring match bởi named group\b(?<word1>\w+)(\s)(?<word2>\w+)\b${word2} ${word1}"one two""two one"
$$Thay ký tự "$" literal\b(\d+)\s?USD$$$1"103 USD""$103"
$&Thay toàn bộ match(\$*(\d*(\.+\d+)?){1})**$&"$1.30""**$1.30**"
$`Thay text trước matchB+$`"AABBCC""AAAACC"
$'Thay text sau matchB+$'"AABBCC""AACCCC"
$+Thay group cuối cùng capturedB+(C+)$+"AABBCCDD"AACCDD
$_Thay toàn bộ input stringB+$_"AABBCC""AAAABBCCC