manish.rathi has asked for the wisdom of the Perl Monks concerning the following question:

1) $line =~ /^(^,*),(.*)$/)

2) $flat_file =~ m!^.*/(^/+)$!){ $flat_file = $1;}

I dont understand these two regex syntax. Pls explain.

Replies are listed 'Best First'.
Re: pls explain following syntax
by ikegami (Patriarch) on Apr 05, 2009 at 06:22 UTC

    You'll have better luck if you put your code in <c>...</c> tags.

    $line =~ /^([^,]*),(.*)$/ matches any string with a comma in it. It captures everything before the first comma, and everything after the first comma.

    $flat_file =~ m!^.*/([^/]+)$!){ $flat_file = $1;} should be replaced with

    use File::Basename qw( fileparse ); $flat_file = fileparse($flat_file);

    File::Basename

Re: pls explain following syntax
by Anonymous Monk on Apr 05, 2009 at 06:38 UTC
    Have you read perlintro yet?
    use YAPE::Regex::Explain; print YAPE::Regex::Explain->new(qr/^([^,]*),(.*)$/)->explain; print YAPE::Regex::Explain->new(qr!^.*/([^/]+)$!)->explain; __END__ The regular expression: (?-imsx:^([^,]*),(.*)$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [^,]* any character except: ',' (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- , ',' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ---------------------------------------------------------------------- The regular expression: (?-imsx:^.*/([^/]+)$) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ^ the beginning of the string ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- / '/' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- [^/]+ any character except: '/' (1 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------