Sed没有给我预期的结果。我想得到第二组的输出,但是sed什么也没给我。我在Ubuntu 20.04.3 LTS
上运行了这个命令,而我使用的是sed (GNU sed) 4.7
。但当我在regex101.com上尝试时,它给了我预期的结果。你可以在这里看到。
root@6ab6c9bc0d76:~# cat /etc/issue
Ubuntu 20.04.3 LTS n l
root@6ab6c9bc0d76:~# sed --version
sed (GNU sed) 4.7
Packaged by Debian
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
Paolo Bonzini, Jim Meyering, and Assaf Gordon.
GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.
组2为空。
root@6ab6c9bc0d76:~# echo "https://one-two-three-four-five.dev.domain.com" | sed -E "s/(https?://)([w|-]*)(.*)/Group1: 1nGroup2: 2nGroup3: 3/"
Group1: https://
Group2:
Group3: one-two-three-four-five.dev.domain.com
root@6ab6c9bc0d76:~#
使用GNUsed
,您可以使用
#!/bin/bash
echo "https://one-two-three-four-five.dev.domain.com" |
sed -E "s~(https?://)([[:alnum:]_-]*)(.*)~Group1: 1nGroup2: 2nGroup3: 3~"
输出:
Group1: https://
Group2: one-two-three-four-five
Group3: .dev.domain.com
请参阅在线演示。
在括号表达式中,w
被解析为反斜杠或w
匹配模式。[:alnum:]
POSIX字符类匹配数字或字母,因此,由于w
也匹配下划线,您需要在括号表达式中组合[:alnum:]
和_
,而不是同时匹配-
字符:[[:alnum:]_-]
。请注意,-
必须位于括号表达式的开始/结束处。
我使用~
作为正则表达式分隔符字符,因为正则表达式模式中有/
字符,这有助于避免过度转义。