我正在尝试从字符串中剪切特定内容并将其存储在变量中以便稍后使用。
字符串是:
\\path\shares\Product\Product_Name\Custom\Version\Version_1\Packages\2018-05-31_07-33-12\PRODUCT_NAME_1_SETUP.exe
如何切割大写字母(PRODUCT_NAME_1_SETUP
)并将其存储在powershell中的变量中。
我非常感谢帮助。
路径实际存储在名为path_name的变量中,我尝试以下操作:
$ build_name = [io.path] :: GetFileNameWithoutExtension(' $($路径名)&#39)
但它不起作用。我得到了O / P" $ path_name"只要。 :(
甚至$ build_name =(Get-Item' $($ path_name)')。Basename也失败了。
答案 0 :(得分:2)
如果您只需要从路径中选择文件名 - 最好的方法是使用
[io.path]::GetFileNameWithoutExtension()
它已在这里得到回答:
Removing path and extension from filename in powershell
答案 1 :(得分:0)
使用RegEx的两个变体
$String ="\\path\shares\Product\Product_Name\Custom\Version\Version_1\Packages\2018-05-31_07-33-12\PRODUCT_NAME_1_SETUP.exe"
$string -match "([^\\]+)\.exe$"|out-Null
$matches[1]
sls -input $string -patt "([^\\]+)\.exe$"|%{$_.Matches.groups[1].Value}
解释RegEx:
([^\\]+)\.exe$
1st Capturing Group ([^\\]+)
Match a single character not present in the list below [^\\]+
+ Quantifier — Matches between one and unlimited times,
as many times as possible, giving back as needed (greedy)
\\ matches the character \ literally (case sensitive)
\. matches the character . literally (case sensitive)
exe matches the characters exe literally (case sensitive)
$ asserts position at the end of the string,
or before the line terminator right at the end of the string (if any)