从 bash 中的正则表达式捕获组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38056900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:49:58  来源:igfitidea点击:

Capture group from regex in bash

regexbash

提问by Cheetah

I have the following string /path/to/my-jar-1.0.jarfor which I am trying to write a bash regex to pull out my-jar.

我有以下字符串/path/to/my-jar-1.0.jar,我正在尝试为其编写 bash 正则表达式以提取my-jar.

Now I believe the following regex would work: ([^\/]*?)-\dbut I don't know how to get bash to run it.

现在我相信以下正则表达式会起作用:([^\/]*?)-\d但我不知道如何让 bash 运行它。

The following: echo '/path/to/my-jar-1.0.jar' | grep -Po '([^\/]*?)-\d'captures my-jar-1

以下:echo '/path/to/my-jar-1.0.jar' | grep -Po '([^\/]*?)-\d'捕获my-jar-1

回答by anubhava

In BASH you can do:

在 BASH 中,您可以执行以下操作:

s='/path/to/my-jar-1.0.jar'

[[ $s =~ .*/([^/[:digit:]]+)-[[:digit:]] ]] && echo "${BASH_REMATCH[1]}"

my-jar

Here "${BASH_REMATCH[1]}"will print captured group #1 which is expression inside first (...).

这里"${BASH_REMATCH[1]}"将打印捕获的组 #1,它是第一个内部的表达式(...)

回答by rici

You can do this as well with shell prefix and suffix removal:

您也可以通过删除 shell 前缀和后缀来执行此操作:

$ path=/path/to/my-jar-1.0.jar
# Remove the longest prefix ending with a slash
$ base="${path##*/}"
# Remove the longest suffix starting with a dash followed by a digit
$ base="${base%%-[0-9]*}"
$ echo "$base"
my-jar

Although it's a little annoying to have to do the transform in two steps, it has the advantage of only using Posix features so it will work with any compliant shell.

尽管必须分两步进行转换有点烦人,但它的优点是仅使用 Posix 功能,因此它可以与任何兼容的 shell 一起使用。

Note: The order is important, because the basename cannot contain a slash, but a path component could contain a dash. So you need to remove the path components first.

注意:顺序很重要,因为基本名称不能包含斜杠,但路径组件可以包含破折号。所以你需要先移除路径组件。

回答by Jeff Y

grep -odoesn't recognize "capture groups" I think, just the entire match. That said, with Perl regexps (-P) you have the "lookahead" option to exclude the -\dfrom the match:

grep -o我认为不识别“捕获组”,只识别整个比赛。也就是说,使用 Perl regexps ( -P) 您可以使用“lookahead”选项-\d从匹配中排除:

echo '/path/to/my-jar-1.0.jar' | grep -Po '[^/]*(?=-\d)'

Some reference material on lookahead/lookbehind: http://www.perlmonks.org/?node_id=518444

前瞻/后视的一些参考资料:http://www.perlmonks.org/?node_id =518444