简单的preg_match

时间:2011-07-20 02:50:52

标签: powershell preg-match

我有$ a = 5816.3.big.jpg;

我想先在$ a中看到所有符号“。”

pregmatch可以做什么?你能救我吗?

2 个答案:

答案 0 :(得分:4)

这样的东西?

$a = "5816.3.big.jpg"
$a.split(".")[0]
5816

很抱歉。你想要正则表达式。

$a = "5816.3.big.jpg"
[regex]::matches($a,"^([^\.]+)\.")[0].groups[1].value
5816

或者:

$a = "5816.3.big.jpg"
[void]($a -match "^([^\.]+)\.")
$matches[1]
5816

答案 1 :(得分:1)

这是另一种选择:

PS > "5816.3.big.jpg" -replace '\..+$'
5816

PS > Get-ChildItem -Path .\123 | Foreach-Object { $_.Name -replace '\..+$' }
相关问题