webshark has asked for the wisdom of the Perl Monks concerning the following question:

hello monks... another newb question... searched everywhere for this and still can't find anything that helps me deal with the commas in this.. my script produces a list of names... I need a metacharacter that will just match any string that ends with ,Y
davis,N brown,Y smith,Y,N,B jones,N,Y,B,A clarke,Y,N,Y
if ($string =~ /^brown,Y/gi) {do this} else {do this}

cheers steve

Replies are listed 'Best First'.
Re: regular expressions with commas?
by ambrus (Abbot) on Dec 10, 2005 at 10:46 UTC

    If you want to accept ^Y only at the end of the string (except for an optional final newline), you need the dollar sign: $string =~ /,Y$/

      you need the dollar sign: $string =~ /,Y$/

      read it in perldoc perlre right on line 59.

      Cheers, Sören

Re: regular expressions with commas?
by osunderdog (Deacon) on Dec 10, 2005 at 11:02 UTC

    Well, here is what I would start with:

    use strict; while(<DATA>) { #using re match. # match beginning, anything except comma, # followed by a comma and 'Y' if(m/^([^,]+),Y/) { print "RE Name:" . $1 . "\n"; } } __DATA__ davis,N brown,Y smith,Y,N,B jones,N,Y,B,A clarke,Y,N,Y

    Or Perhaps:

    use strict; while(<DATA>) { my @data = split(','); if($data[1] eq 'Y') { print "RE Name:" . $data[0] . "\n"; } } __DATA__ davis,N brown,Y smith,Y,N,B jones,N,Y,B,A clarke,Y,N,Y

    Hazah! I'm Employed!

      Those both match given the string "Blah,Y,X", which has a substring ending with ",Y", but doesn't itself end with that sequence. (It isn't completely clear to me what the OP wanted, though...)

        I interpreted it as meaning a ",Y" after the name string. Many others interpreted it as meaning a ",Y" at the end of the line. The OP wasn't clear so I figure I would share my interpretation.

        Hazah! I'm Employed!

      cheers guys... works a treat..
Re: regular expressions with commas?
by Anonymous Monk on Dec 10, 2005 at 11:37 UTC
    There's written in the book, that metacharacter $ match end of line in regular expressions. So you can use /,Y$/ for example. Greetings Richard