【问题标题】:REGEX: match directory lines正则表达式:匹配目录行
【发布时间】:2016-06-04 03:33:43
【问题描述】:

我有一个目录列表:

/media/nosthertus/music/oor no scared.mp3
/media/nosthertus/OST/data/music/zone4_1.ogg
/media/nosthertus/OST/data/music/zone4_2.ogg
/media/nosthertus/music/Epic Hybrid _ Alex Moukala - Reborn _ Orchestral Action Rock _ EpicMusicVN.mp3

我不太擅长REGEX,所以我尝试使用这个表达式:

/\/(\w+|(\w+\s))\..{3}/g

有一个regex101 会话有更多关于我尝试匹配here 中的目录的示例

请解释我做错了什么..

编辑:我想匹配包含在字符串中的整个目录..例如

Some random string text here
/dir/to/folder/or/file.ext   //Match this whole DIR

【问题讨论】:

  • 你想匹配什么?你想要什么输出?
  • @rock321987 我已经编辑了问题
  • 你能澄清一下你想在/media/nosthertus/OST/data/music/zone4_2.ogg中匹配什么吗?
  • @rock321987 我想匹配所有内容,你可以在 regex101 中看到其他不是目录的东西,我的目标是在一个 javascript 函数中使用它,该函数将从字符串中提取所有目录
  • this好吗?

标签: regex


【解决方案1】:

如果您想匹配所有目录(我假设任何以/ 开头并包含至少一个/ 的内容都是目录)

^\/[^\/]*\/.*$

Regex Demo

如果你愿意,你可以使用

^\/.*$

Regex Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2016-02-16
    • 2017-07-26
    • 1970-01-01
    相关资源
    最近更新 更多