How do I extract abc from 'abc' using perl regular expression?
如何使用perl正则表达式从'abc'中提取abc?
I tried
echo "'abc'" | perl -ne 'if(/\'(.*)\'/) {print $1}'
but it shows -bash: syntax error near unexpected token `('
但它显示-bash:意外令牌附近的语法错误`(''
Thanks in advance for your reply.
在此先感谢您的回复。
6 个解决方案
#1
7
This is not a perl problem, this is a shell problem: you cannot include single quotes into single quotes.
这不是一个perl问题,这是一个shell问题:你不能将单引号包含在单引号中。
You have to replace each single quote with '\''
(end of single quotes, escaped single quote, start of signle quotes)
你必须用'\''替换每个单引号(单引号末尾,单引号转义,单引号引号)
echo "'abc'" | perl -ne 'if(/'\''(.*)'\''/) {print $1}'
#2
3
Precede your single-quoted Perl code with a dollar sign to direct bash to use an alternate quoting method that turns-off shell expansion:
在带有美元符号的单引号Perl代码之前,指示bash使用替代引用方法来关闭shell扩展:
echo "'abc'" | perl -ne $'if(/\'(.*)\'/) {print $1}'
#3
2
Well, the cheap way is not to surround your perl statement with single quotes themselves:
好吧,便宜的方法不是用单引号包围你的perl语句:
echo "'abc'" | perl -ne "if(/'(.*)'/) {print $1}"
Shell escaping has odd rules...
壳逃逸有奇怪的规则......
If you really want to do it the "right" way you can end your first single quoted string, put the quote in, and start another one:
如果你真的想以“正确”的方式做到这一点,你可以结束你的第一个单引号字符串,把引号放入,然后开始另一个:
echo "'abc'" | perl -ne 'if(/'\''(.*)'\''/) {print $1}'
#4
2
choroba's answer solves the exact problem. For a generalised solution to any quoting problem, use String::ShellQuote:
choroba的答案解决了确切的问题。对于任何引用问题的通用解决方案,请使用String :: ShellQuote:
$ alias shellquote='perl -E'\''
use String::ShellQuote qw(shell_quote);
local $/ = undef;
say shell_quote <>;
'\'''
$ shellquote
user input → if(/'(.*)'/) {print $1}␄
perl output → 'if(/'\''(.*)'\''/) {print $1}'
#5
1
you need to escape your single qoute with '\''
你需要用'\''来逃避你的单身qoute
echo "'abc'" | perl -ne 'if( /'\''(.*)'\''/ ){print $1}'
echo“'abc'”| perl -ne'if(/'\''(。*)'\''/){print $ 1}'
#6
1
You have a shell quoting problem, not a Perl problem.
你有shell引用问题,而不是Perl问题。
This is a good use for sed
:
这对sed很有用:
echo "'abc'" | sed "s/'//g"
#1
7
This is not a perl problem, this is a shell problem: you cannot include single quotes into single quotes.
这不是一个perl问题,这是一个shell问题:你不能将单引号包含在单引号中。
You have to replace each single quote with '\''
(end of single quotes, escaped single quote, start of signle quotes)
你必须用'\''替换每个单引号(单引号末尾,单引号转义,单引号引号)
echo "'abc'" | perl -ne 'if(/'\''(.*)'\''/) {print $1}'
#2
3
Precede your single-quoted Perl code with a dollar sign to direct bash to use an alternate quoting method that turns-off shell expansion:
在带有美元符号的单引号Perl代码之前,指示bash使用替代引用方法来关闭shell扩展:
echo "'abc'" | perl -ne $'if(/\'(.*)\'/) {print $1}'
#3
2
Well, the cheap way is not to surround your perl statement with single quotes themselves:
好吧,便宜的方法不是用单引号包围你的perl语句:
echo "'abc'" | perl -ne "if(/'(.*)'/) {print $1}"
Shell escaping has odd rules...
壳逃逸有奇怪的规则......
If you really want to do it the "right" way you can end your first single quoted string, put the quote in, and start another one:
如果你真的想以“正确”的方式做到这一点,你可以结束你的第一个单引号字符串,把引号放入,然后开始另一个:
echo "'abc'" | perl -ne 'if(/'\''(.*)'\''/) {print $1}'
#4
2
choroba's answer solves the exact problem. For a generalised solution to any quoting problem, use String::ShellQuote:
choroba的答案解决了确切的问题。对于任何引用问题的通用解决方案,请使用String :: ShellQuote:
$ alias shellquote='perl -E'\''
use String::ShellQuote qw(shell_quote);
local $/ = undef;
say shell_quote <>;
'\'''
$ shellquote
user input → if(/'(.*)'/) {print $1}␄
perl output → 'if(/'\''(.*)'\''/) {print $1}'
#5
1
you need to escape your single qoute with '\''
你需要用'\''来逃避你的单身qoute
echo "'abc'" | perl -ne 'if( /'\''(.*)'\''/ ){print $1}'
echo“'abc'”| perl -ne'if(/'\''(。*)'\''/){print $ 1}'
#6
1
You have a shell quoting problem, not a Perl problem.
你有shell引用问题,而不是Perl问题。
This is a good use for sed
:
这对sed很有用:
echo "'abc'" | sed "s/'//g"