Reputation: 40778
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
Upvotes: 2
Views: 80
Reputation: 67900
Text::ParseWords
can handle quoted delimiters:
use strict;
use warnings;
use Text::ParseWords;
my $str = "test 'a/b' c/ xxx /";
my ($first) = quotewords('/', 1, $str);
print $first;
Output:
test 'a/b' c
Upvotes: 4
Reputation: 35208
my $str="test 'a/b' c/ xxx /";
$str =~ m{^((?>[^'/]+|'[^']*')*)/};
print $1;
Or if strings can contain escaped single quotes:
$str =~ m{^((?>[^'/]+|'(?>[^'\\]+|\\.)*')*)/}x;
Upvotes: 3
Reputation: 89584
you can try this:
$str =~ /^((?>[^'\/]+|'[^']*')*)\//;
Upvotes: 4
Reputation: 131
If your string data is always "A/B/" then you could use the following:
#!/usr/bin/perl -w
my $str = "test 'a/b' c/ xxx /";
$str =~ m/(.*)\/(.*)\/$/;
print "$1\n";
Upvotes: 0