user1234
user1234

Reputation: 97

Powershell to get a DLL name out of it's full path

I have a string "....\xyz\abc\0.0\abc.def.ghi.jkl.dll" am trying to get the value of a "abc.def.ghi.jkl.dll" into a variable using powershell.

I am totally new to regex and PS and kinda confused on how to get this done. I read various posts about regex and I am unable to get anything to work

Here is my code,

$str = "..\..\xyz\abc\0.0\abc.def.ghi.jkl.dll"

$regex = [regex] '(?is)(?<=\b\\b).*?(?=\b.dll\b)'
$result = $regex.Matches($str)
Write-Host $result

I would like to get "abc.def.ghi.jkl.dll" into $result. Could someone please help me out

Upvotes: 0

Views: 185

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626932

You can use the following regex:

(?is)(?<=\\)[^\\]+\.dll\b

See regex demo

And no need to use Matches, just use a -match (or Match).

Explanation:

  • (?<=\\) - make sure there is a \ right before the current position in string
  • [^\\]+ - match 1 or more characters other than \
  • \.dll\b - match a . symbol followed by 3 letters dll that are followed by a trailing word boundary.

Powershell:

$str = "..\..\xyz\abc\0.0\abc.def.ghi.jkl.dll"
[regex]$regex = "(?is)(?<=\\)[^\\]+\.dll\b"
$match = $regex.match($str)  
$result = ""
if ($match.Success)  
{            
    $result = $match.Value
    Write-Host $result
}

enter image description here

Upvotes: 1

Related Questions