PowerShell 中的正则表达式匹配 [英] Regular expression matching in PowerShell

查看:47
本文介绍了PowerShell 中的正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种优雅的单线来执行以下操作?

Is there an elegant one-liner for doing the following?

$myMatch = "^abc(.*)"
$foo -match $myMatch
$myVar = $matches[1]

我对 $myVar 变量感兴趣...

I'm interested in the $myVar variable...

推荐答案

只需使用-replace:

$foo = 'abcDEF'
$myMatch = "^abc(.*)"
$myVar = $foo -replace $myMatch,'$1'
#$myVar contains DEF

这篇关于PowerShell 中的正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆