Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract a string enclosed in single quotes in Perl?

Tags:

regex

perl

How do I extract abc from 'abc' using a Perl regular expression?

I tried

echo "'abc'" | perl -ne 'if(/\'(.*)\'/) {print $1}'

but it showed:

-bash: syntax error near unexpected token `('

like image 953
Fashandge Avatar asked Dec 03 '22 05:12

Fashandge


1 Answers

This is not a Perl problem; this is a shell problem: you cannot include single quotes into single quotes.

You have to replace each single quote with '\'' (end of single quotes, escaped single quote, and start of single quotes):

echo "'abc'" | perl -ne 'if(/'\''(.*)'\''/) {print $1}'
like image 73
choroba Avatar answered Jan 31 '23 14:01

choroba