例如,我有一个这样的网址 - > http://google.com/test/to_be_extracted.html
我想拆分此网址并仅获取to_be_extracted
部分。我想要排除http://google.com/test/
和.html
部分。
如何使用GREP或SED设置正则表达式模式?
答案 0 :(得分:2)
您可以使用:
$ echo 'http://google.com/test/to_be_extracted.html' | sed -r 's#.*\/([^.]+).*#\1#'
to_be_extracted
sed -r ' # -r switch enables Extended Regular expressions
s # Using substitution flag
# # Using # as delimiter since you have `/` in your lines
.*\/ # Match everything greedily until you see last `/`.
([^.]+) # Create a capture group to capture everything until you see a literal .
.* # Followed by everything else
# # Another delimiter
\1 # Print the captured group
#' # Final delimiter
答案 1 :(得分:0)
为什么您需要解决方案来使用不必要的工具?
basename "$url" .html
会做你需要的,平凡而透明的。