Reputation: 135
still dealing with regular expressions. I've got file that will contain a combination of lines. I need to pick up everything from A to the right. If character A is inside // - it's not counted. If A is the first character of the line - everything should be picked up. There would be three scenarios:
my $str = 'some before/some.A;hi99/some after A text goes here'
my $str1 = 'A some before/some.A;hi99/some after A text goes here'
my $str2 = 'some before/some.A;hi99/some after
Otput should be:
A text goes here
A some before/some.A;hi99/some after A text goes here
nothing
I can match up A inside // but I don't know if it's possible to check other two conditions inside regex?
$str =~ /([^\/]+.*[A]+.*[^\/]+)/g;
Any ideas would be much appreciated.
Upvotes: 0
Views: 74
Reputation: 35208
Using split
and join
to do an amusing transformation:
use strict;
use warnings;
while (<DATA>) {
chomp;
my $odd = 0;
my $pass = 0;
my $line = join '/', grep {$pass || (($odd ^= 1) && s/.*?A/A/ && ++$pass)} split '/';
print $line
? "(pass) - $line\n"
: "(fail)\n";
}
__DATA__
some before/some.A;hi99/some after A text goes here
A some before/some.A;hi99/some after A text goes here
some before/some.A;hi99/some after
Outputs
(pass) - A text goes here
(pass) - A some before/some.A;hi99/some after A text goes here
(fail)
Upvotes: 2