Deano Deano - 7 months ago 27
Perl Question

Perl oneliner regex

I have created this regex script online, to strip bracket out of string

[/apache/20160419/20160419-0643/20160419-064309-VxYLvX8AAAEAABumDlIAAAAG]


I'm wondering if it is possible to run it on the shell as "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;
}


I have tried

cat file | perl -wnE'say /((?:\\/[\\w\\.\\-]+)+)/g'
Unmatched ( in regex; marked by <-- HERE in m/(( <-- HERE ?:\\/ at -e line

Answer

You can use sed, but perl works too:

echo "[foo]" | perl -pe 's/[\[\]]//g'

Though i'm not sure why you want to use such a complicated regex just to remove braces.