Reputation: 5577
Should be easy, but this regexp is not working (Perl):
^(.*?)-(.*?)(-(.*)|$)
a - b - c (I want $1=a, $2=b, $3=c)
a - b (I want $1=a, $2=b)
abc
Any ideas welcome.
You can try it here:
https://regex101.com/r/kP0fN9/2
Upvotes: 1
Views: 53
Reputation: 91385
How about:
use strict;
use warnings;
use Data::Dump qw(dump);
while(<DATA>) {
chomp;
my @res = $_ =~ /([^-]+)/g;
dump@res;
}
__DATA__
a - b - c
a - b
Output:
("a ", " b ", " c")
("a ", " b")
Upvotes: 1
Reputation: 174696
Seems like you want something like this,
^[^-]*-(?:[^-]*-)?[^-]*$
OR
^[^-\n]*-(?:[^-\n]*-)?[^-\n]*$
Update:
^(.*?)\s*-\s*(.*?)\s*(?:-\s*(.*)|$)
Upvotes: 2