我正在尝试从字符串中剪切特定的内容,并将其存储在一个变量中以供以后使用。
字符串为:
\\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('$($path_name)')
但它不起作用。我只得到O/P "$path_name“。:(
甚至$build_name=(Get-Item '$($path_name)').Basename也失败了。
发布于 2018-06-04 19:15:54
如果您只需要从路径中选择文件名-最好的方法是使用[io.path]::GetFileNameWithoutExtension(),这里已经回答了这个问题:Removing path and extension from filename in powershell
发布于 2018-06-04 19:45:59
使用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)https://stackoverflow.com/questions/50679100
复制相似问题