【问题标题】:Get filename without the extension with regex使用正则表达式获取不带扩展名的文件名
【发布时间】:2022-11-19 22:18:17
【问题描述】:

我需要找到一种方法来获得不带扩展名的特定文件名的匹配项。

Test App 0.2.1.exe
dsdsad

Test App 0.2.1.dmg
Test App 0.2.1

目前我试过 ^\S+\.(?:dmg)$ 但它不匹配。

所以基本上我想得到以下结果:

Test App 0.2.1(不带扩展名)。只是为了获取名称,以便我可以使用该名称保存在 GitHub Actions 中的变量中。目前我正在使用https://github.com/marketplace/actions/actions-ecosystem-action-regex-match

链接到 regex101:https://regex101.com/r/hY7Xfp/1

【问题讨论】:

  • 像这样? ^(\S.*)\.dmg$ 有捕获组吗? regex101.com/r/TYR21D/1
  • 或者只是(.*)\.dmg$甚至(.*)\.dmg
  • 或者更普遍的(.*)\.[^.]*$

标签: regex


【解决方案1】:

尝试 :

.+(?=.[a-z0-9]+)

上面的表达式为您匹配文件名,而不管它们的扩展名(不匹配扩展名本身)。

如果你只想匹配带有 .dmg 扩展名的文件名(不匹配 .dmg 本身),你可以试试这个:

.+(?=(.dmg))

注意:如果要匹配其他扩展名的文件名,只需更改上面表达式中的.dmg 部分即可。

例如 :

.+(?=(.exe)) #Matches file names with .exe extension

.+(?=(.jpg)) #Matches file names with .jpg extension

.+(?=(.mp3)) #Matches file names with .mp3 extension

  and so on ...

我在这个链接中对你有一点。

RegExr.com |一探究竟 。

我希望这有帮助 :)

【讨论】:

  • {1,} 只是+ 的冗长表述
猜你喜欢
  • 2019-02-19
  • 2013-03-18
  • 2020-03-11
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
相关资源
最近更新 更多