根据给定的格式化掩码,Powershell中是否有一个剪切值的函数?
让我们使用例如$test = "Value 14, Code 57"
Cut-Substring("Value $val, Code $code",$test)
结果我希望收到$val = 14
和$code = 57
。
如果没有,是否有更强大的工具可以访问给定标签旁边的字段 ?
答案 0 :(得分:5)
$test = "Value 14, Code 57"
$test -match 'Value (\d+), Code (\d+)'
$matches[1] #14
$matches[2] #57
正则表达式的强大功能应该允许您根据需要进行调整。
答案 1 :(得分:0)
替代方案。
$test = "Value 14, Code 57"
$val,$code=$test -split ',' | ForEach {($_.Trim() -split ' ')[1]}
'$val={0} and $code={1}' -f $val,$code
# Prints
# $val=14 and $code=57