解释linux正则表达式获取文件名 [英] explain the linux regex for getting filename

查看:327
本文介绍了解释linux正则表达式获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我解释为什么使用这种方式获取文件名. 例如:文件为:

Can any guys kindly help me explain why getting filename using this way. for example: the file is:

fileName = "/Users/test/am01/output/output.log"
fileName=${fileName##*/}

然后我们得到了

fileName = output.log

你们能帮我解释一下## *是什么意思,我对这个正则表达式很困惑.

Could you guys help me explain what does ##* means, I'm so confused on this regex expression.

此外,还有另一个类似的例子:

Additionally, there is another example like this:

filename="testdata.done"
echo ${filename%.done}
then we got test data

%是什么意思,我从未见过此正则表达式表达式.

What does % mean, I've never seen this regex expression.

推荐答案

这不是reqexp,它是普通的老式bash魔术. 指南说:

This is not reqexp, it it plain old bash magic. This guide says:

${string##substring}

从$ string的前面删除$ substring的最长匹配项.

Deletes longest match of $substring from front of $string.

此处的星号是通配符,因此*/表示删除以/结尾的最长子串.

The star is here a wildcard, so the */ means, delete the longest substring that ends with /.

"/Users/test/am01/output/"在您的情况下.

问题的第二部分:

${string%substring}

从$ string的后面删除$ substring的最短匹配项.

Deletes shortest match of $substring from back of $string.

这篇关于解释linux正则表达式获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆