我已在线创建此正则表达式脚本,从字符串[/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG]
我想知道是否可以在shell上运行它作为“perl oneliner”
#!/usr/bin/perl
# URL that generated this code:
# http://txt2re.com/index.php3?s=[/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG]&2
$txt='[/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG]';
$re1='.*?'; # Non-greedy match on filler
$re2='((?:\\/[\\w\\.\\-]+)+)'; # Unix Path 1
$re=$re1.$re2;
if ($txt =~ m/$re/is)
{
$unixpath1=$1;
print $unixpath1;
}
我试过了
cat file | perl -wnE'say /((?:\\/[\\w\\.\\-]+)+)/g'
Unmatched ( in regex; marked by <-- HERE in m/(( <-- HERE ?:\\/ at -e line
答案 0 :(得分:3)
似乎过于复杂,相当于:
$txt =~ s/(^\[|\]$)//g;
或者,如果提供整个网址,并作为一个班轮:
perl -ne 'print m/\[([^]]+)\]/;'
答案 1 :(得分:2)
我不确定你想要什么,但这会从命令行提供的字符串中删除所有方括号
$ perl -E "say shift =~ tr/[]//dr" [/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG]
/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG
或者这将打印不是方括号的第一个字符序列,给定此数据的结果相同
$ perl -E "say shift =~ / ( [^\[\]]+ ) /x" [/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG]
/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG
答案 2 :(得分:2)
您可以使用sed
,但perl也适用:
echo "[foo]" | perl -pe 's/[\[\]]//g'
虽然我不确定你为什么要使用如此复杂的正则表达式来删除大括号。