我在以两位数开头的 unix 路径中有文件名......如何在没有扩展名的情况下提取名称
/this/is/my/path/to/the/file/01filename.ext
应该filename
我目前有[^/]+(?=.ext$)
所以我得到01filename
,但是我如何摆脱前两位数字?
您可以在已有内容的前面添加一个后视,查找两位数:
(?<=dd)[^/]+(?=.ext$)
这仅在您正好有两位数时才有效!不幸的是,在大多数正则表达式引擎中,不可能在回溯中使用 *
或 +
等量词。
-
(?<=dd)
- 在比赛前检查两位数 -
[^/]+
- 匹配 1 个或多个字符,/
除外 -
(?=.ext$)
- 检查比赛背后的.ext
试试这个:
/dd(.*?).w{3}$
解释:
/dd
: 斜杠后跟两位数字
(.*?)
: 捕获
.w{3}
:一个点后跟三个字母
$
: 字符串结尾
它在Expresso上对我有用
考虑以下正则表达式...
(?<=d{2})[^/]+(?=.ext$)
祝你好运!
一个更通用的正则表达式:
(?:^|/)[d]+([^.]+).[w.]+$
解释:
(?: group, but do not capture:
^ the beginning of the string
| OR
/ '/'
) end of grouping
[d]+ any character of: digits (0-9) (1 or more
times (matching the most amount possible))
( group and capture to 1:
[^.]+ any character except: '.' (1 or more
times (matching the most amount
possible))
) end of 1
. '.'
[w.]+ any character of: word characters (a-z, A-
Z, 0-9, _), '.' (1 or more times
(matching the most amount possible))
$ before an optional n, and the end of the
string