in reply to Regex to capture every readable character except COMMA

Basically you can make it a two-step method as well which might not be the most elegant/efficient solution but at least it's working and it's damn simple.
#!/usr/bin/perl -w use warnings; use strict; my $var = "abc,d"; if($var =~ /,/) { print "not matches \n"; } elsif($var =~ /(.*)/) { print "matches \n Captured: $1\n"; } $var = "abcd"; if($var =~ /,/) { print "not matches \n"; } elsif($var =~ /(.*)/) { print "matches \n Captured: $1\n"; } exit 0;
Output:
not matches matches Captured: abcd

Replies are listed 'Best First'.
Re^2: Regex to capture every readable character except COMMA
by AnomalousMonk (Archbishop) on Jan 09, 2014 at 19:02 UTC
    if($var =~ /,/) { print "not matches \n"; } elsif($var =~ /(.*)/) { print "matches \n Captured: $1\n"; }

    The intent here seems to be to capture the entire string if it contains no comma. But  $var already holds the entire string! Why 'capture' it again?

    Furthermore, a confounding subtlety lurks at the heart of the seemingly simple, innocent  /(.*)/ regex: the  . (dot) metaoperator matches everything except a newline: it will only capture up to the first newline, if present. You have just built a bug into your code. (This subtlety is the rationale for the PBP injunction to always use the  /s "dot matches all" modifier with every regex.)