Reputation: 307
In a file i have many lines like this
Nom de l'objet : C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico
I just want extension of the file (here ico). some extension may have more than 3 characters. With awk, i can do, but in powershell... Thanks for help.
Upvotes: 0
Views: 999
Reputation: 18176
Rather than use RegEx, why not use the [System.IO.Path] class to get the extension:
$str = "Nom de l'objet : C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico"
$filename=($str -split ':',2)[1]
[System.IO.Path]::GetExtension($filename)
Upvotes: 3
Reputation: 24585
$str = "Nom de l'objet : C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico"
if ( $str.LastIndexOf(".") -gt -1 ) {
$str.Substring($str.LastIndexOf(".") + 1)
}
# Outputs "ico"
Upvotes: 1
Reputation: 3918
With some regex:
$str = "Nom de l'objet : C:\ProgramData\Microsoft\Device Stage\Task\{07deb856-fc6e-4fb9-8add-d8f2cf8722c9}\netfol.ico"
$str -match "\.(.{3,})$"
$extension = $Matches[1]
$extension #ico
\.(.{3,})$
matches at least 3 chars after the last dot before the very end of the line.
Upvotes: 2