简体   繁体   中英

Can I use condition in regular expression in Perl?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM