string 删除变量开头和结尾的子串匹配模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17129050/
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
Remove substring matching pattern both in the beginning and the end of the variable
提问by bobylapointe
As the title says, I'm looking for a way to remove a defined pattern both at the beginning of a variable and at the end. I know I have to use #
and %
but I don't know the correct syntax.
正如标题所说,我正在寻找一种方法来删除变量开头和结尾的已定义模式。我知道我必须使用#
,%
但我不知道正确的语法。
In this case, I want to remove http://
at the beginning, and /score/
at the end of the variable $line
which is read from file.txt
.
在这种情况下,我想http://
在开头和/score/
结尾处删除$line
从file.txt
.
回答by
Well, you can't nest ${var%}
/${var#}
operations, so you'll have to use temporary variable.
好吧,您不能嵌套${var%}
/${var#}
操作,因此您必须使用临时变量。
Like here:
像这儿:
var="http://whatever/score/"
temp_var="${var#http://}"
echo "${temp_var%/score/}"
Alternatively, you can use regular expressions with (for example) sed:
或者,您可以将正则表达式与(例如)sed 一起使用:
some_variable="$( echo "$var" | sed -e 's#^http://##; s#/score/$##' )"
回答by jaypal singh
$ var='https://www.google.com/keep/score'
$ var=${var#*//} #removes stuff upto // from begining
$ var=${var%/*} #removes stuff from / all the way to end
$ echo $var
www.google.com/keep
回答by Charles Duffy
There IS a way to do it one step using only built-in bash functionality (no running external programs such as sed
) -- with BASH_REMATCH
:
有一种方法可以仅使用内置的 bash 功能(不运行诸如 的外部程序sed
)来一步完成它——使用BASH_REMATCH
:
url=http://whatever/score/
re='https?://(.*)/score/'
[[ $url =~ $re ]] && printf '%s\n' "${BASH_REMATCH[1]}"
This matches against the regular expression on the right-hand side of the =~
test, and puts the groups into the BASH_REMATCH
array.
这与=~
测试右侧的正则表达式匹配,并将组放入BASH_REMATCH
数组中。
That said, it's more conventional to use two PE expressions and a temporary variable:
也就是说,使用两个 PE 表达式和一个临时变量更为传统:
shopt -s extglob
url=http://whatever/score/
val=${url#http?(s)://}; val=${val%/score/}
printf '%s\n' "$val"
...in the above example, the extglob
option is used to allow the shell to recognized "extglobs" -- bash's extensions to glob syntax (making glob-style patterns similar in power to regular expressions), among which ?(foo)
means that foo
is optional.
...在上面的示例中,该extglob
选项用于允许 shell 识别“extglobs”——bash 对 glob 语法的扩展(使 glob 样式模式在功能上类似于正则表达式),其中?(foo)
意味着这foo
是可选的。
By the way, I'm using printf
rather than echo
in these examples because many of echo
's behaviors are implementation-defined -- for instance, consider the case where the variable's contents are -e
or -n
.
顺便说一下,我在这些示例中使用printf
而不是echo
,因为 的许多echo
行为是实现定义的——例如,考虑变量的内容是-e
或 的情况-n
。
回答by Gilles Quenot
You have to do it in 2 steps :
您必须分两步完成:
$ string="fooSTUFFfoo"
$ string="${string%foo}"
$ string="${string#foo}"
$ echo "$string"
STUFF
回答by michael501
how about
怎么样
export x='https://www.google.com/keep/score';
var=$(perl -e 'if ( $ENV{x} =~ /(https:\/\/)(.+)(\/score)/ ) { print qq();}')