Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

reg exp to find presence of value in a list of comma seperated list

by kgforperl (Novice)
on Nov 17, 2009 at 20:35 UTC ( #807785=perlquestion: print w/ replies, xml ) Need Help??
kgforperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Can some one help me to write a regular expression to find whether a value(ex: value1) is present in the list of comma-separated values(ex: value3,value7,value1,value5)

Comment on reg exp to find presence of value in a list of comma seperated list
Re: reg exp to find presence of value in a list of comma seperated list
by kennethk (Monsignor) on Nov 17, 2009 at 20:42 UTC
    I suspect that for whatever you are doing, you would be better off splitting first or better yet using a CSV module (like Text::CSV), but it's fairly trivial to check with:

    /(?:^|,)value1(?:,|$)/

    where I've used non-capturing groups with alternators to make it insensitive to whether it is next to commas or the start/end of the list. See perlretut.

      Thank you kennethk. I am learning perl now. I found that this one also is working $commalist =~ /,$item,/. I know there might be more than one way to do this, but if you don't mind can you tell me the difference between the one you suggested and this one

        First, welcome to the Perl community. I hope you get as much enjoyment out of working with Perl as I have.

        Your regular expression misses the case where either the first or last element of your list is the element you wish to check for. Consider:

        #!/usr/bin/perl use strict; use warnings; my $csv = 'value1,value2,value3'; my $value = 'value1'; if ($csv =~ /(?:^|,)$value(?:,|$)/) { print "Yes\n"; } else { print "No\n"; } if ($csv =~ /[,]$value[,]/) { print "Yes\n"; } else { print "No\n"; }

        Mine catches those cases since the first group ((?: ... )) matches on either the start of the string (^) or a comma and the second group matches on either the end of the string ($) or a comma. Since value1 is not surrounded by commas, yours will not find it. Note that both cases will fail if there is white space in the csv that is not in the $value.

        You might also note there is no need to define the single-element character class [,] as opposed to just using the raw comma ,.

        As a side note, please read Markup in the Monastery, since by not wrapping your code in code tags, you inadvertently converted [,] into a link - when this happens, monks frequently misread your intention.

Re: reg exp to find presence of value in a list of comma seperated list
by sherab (Scribe) on Nov 17, 2009 at 21:16 UTC
    If you're on perl 5.10 and greater, you could put it into an array and then check using perl smart operator.A good example of how to use this is here. It's not using a regexp but it might be helpful if you are trying to see if "x" value is in an array of values.
Re: reg exp to find presence of value in a list of comma seperated list
by Marshall (Prior) on Nov 17, 2009 at 22:24 UTC
    Here is another solution below.

    You didn't mention whether spaces are allowed in the comma separated fields or not or for that matter embedded commas within some quoted thing in the comma separated fields.

    Here I allow spaces around the values assuming that the values are single tokens and do not contain spaces. This uses split to create a list of tokens (without commas or spaces) and the the scalar value of grep{} is used to count the number of times that value1 is seen. value1 must not have any characters before or after it. That is what the "carrot" and "dollar sign" do. Otherwise something like value1value2, value3 would match.

    The regex stuff is a bit easier here, but it runs slower. If you have some comma separated thing that can come from a spreadsheet or whatever, you should consider one of the parser modules for CSV files. Search on CSV and you will find several.

    #!usr/bin/perl -w use strict; my $line = "value3,value7, value1 ,value5"; #to show what the split does... print split(/[, ]/,$line),"\n"; if (grep {/^value1$/} split(/[, ]/,$line) ) { print "value1 found!\n"; } #prints: #value3value7value1value5 #value1 found!
Re: reg exp to find presence of value in a list of comma seperated list
by vitoco (Pilgrim) on Nov 18, 2009 at 02:42 UTC
Re: reg exp to find presence of value in a list of comma seperated list
by shawnhcorey (Pilgrim) on Nov 18, 2009 at 13:52 UTC

    There is no regular expression that can parse CSV. Use a module, like Text::CSV, to do it for you. And yes, there is a significant difference between CSV and a list of values separated by commas.

    Also, there is no standard for CSV files. There is a standard for CSV MIME type: RFC 4180.

Re: reg exp to find presence of value in a list of comma seperated list
by Anonymous Monk on Nov 18, 2009 at 14:40 UTC
    Depending on the OP's needs, Perl's grep function might be useful.

    http://perldoc.perl.org/functions/grep.html

    Just search the list returned by grep to see if it's empty or not.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://807785]
Approved by Corion
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2014-07-26 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls