首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >powershell中的cut equivalent命令是什么

powershell中的cut equivalent命令是什么
EN

Stack Overflow用户
提问于 2018-06-04 19:03:08
回答 2查看 2.6K关注 0票数 1

我正在尝试从字符串中剪切特定的内容,并将其存储在一个变量中以供以后使用。

字符串为:

代码语言:javascript
复制
\\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也失败了。

EN

回答 2

Stack Overflow用户

发布于 2018-06-04 19:15:54

如果您只需要从路径中选择文件名-最好的方法是使用[io.path]::GetFileNameWithoutExtension(),这里已经回答了这个问题:Removing path and extension from filename in powershell

票数 2
EN

Stack Overflow用户

发布于 2018-06-04 19:45:59

使用RegEx的两个变体

代码语言:javascript
复制
$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:

代码语言:javascript
复制
([^\\]+)\.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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50679100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档