PowerShell 中的正则表达式操作全解析
1. -replace 运算符
- 字符串替换 :在 PowerShell 里,
-replace运算符可用于替换字符串。例如:
$MyString = 'May: The sunshine is mellow.'
$MyString -replace '\bm(\w+)\b', 'y$1'
$MyString -replace '([aeiou])'
运行上述代码,第一个 $MyString -replace '\bm(\w+)\b', 'y$1' 会将以 m 开头的单词替换为 y 加上原单词剩余部分,输出 yay: The sunshine is yellow. ;第二个 $MyString -replace '([aeiou])' 会替换元音字母,输出 My: Th snshn s mllw. 。
- 使用脚本块 : -replace 运算符还能接受脚本块,实现更高级的字符串操作。示例如下:
$MyString = 'This sentEnce has infixed cApital leTTErs.'
$
超级会员免费看
订阅专栏 解锁全文
545

被折叠的 条评论
为什么被折叠?



