Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match text up to given character but excluding character occurrence within single quotes in Perl

Tags:

regex

perl

I am trying to extract text from a string up to the next occurrence of a /, not counting slashes that occur within single quotes. For example

my $str="test 'a/b' c/ xxx /";
$str =~ /(.*?)\//;
print "$1\n";

gives

test 'a

whereas I would like to get:

test 'a/b' c
like image 867
Håkon Hægland Avatar asked Dec 12 '25 23:12

Håkon Hægland


1 Answers

you can try this:

$str =~ /^((?>[^'\/]+|'[^']*')*)\//;
like image 74
Casimir et Hippolyte Avatar answered Dec 15 '25 19:12

Casimir et Hippolyte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!