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

Hello Fellow Monks,

I'm just wondering how you can use regex to determine if a array contains certain things your looking for.

I want to inspect a array of numbers (ids) that is created when a user checks a list of html checkboxes.

So I was wondering how can I check if this array of ids contain only numbers and commas.

ex. $INPUT{'checked_ids'} will contain '11, 22, 33, 44, 55'

This input field can only return numbers and no alphabetical characters--therefore its proofing the input.

I tried looking at regex tutorials and examples but couldn't grasp ones that talked about checking arrays?

Thanks!

Replies are listed 'Best First'.
Re: Regex an Array?
by Roy Johnson (Monsignor) on May 02, 2005 at 17:43 UTC
    By array, do you mean "string with multiple occurrences of a pattern"? That's what your example looks like you mean. To ensure that a string is a comma-separated list of digits (and nothing else), the regex would be:
    /^[0-9]+(?:,[0-9]+)*$/;
    You anchor at the front and end to say that the whole string must match the pattern. You group and quantify to indicate repetitions.

    Caution: Contents may have been coded under pressure.
      He had spaces in his input string. I presume he wants you to allows spaces too. If you're going to take his question literally, then /^(?:[0-9,]*)$/ would suffice.
        I didn't notice the spaces until I first saw your earlier solution. This solution isn't quite right unless you accept ",,," as a valid input.

        Caution: Contents may have been coded under pressure.
        Considering the OP named it 'checked_ids' I was more or less interpreting that as meaning that that is where he will store the result of the check, not the actual data that needs to be tested. Dear OP, please enlighten us :-)

        Remember rule one...

Re: Regex an Array?
by ikegami (Patriarch) on May 02, 2005 at 17:40 UTC

    '11, 22, 33, 44, 55' is a string (not an array), so
    $INPUT{'checked_ids'} =~ /^(?:\s*[0-9]+\s*,)\s*[0-9]+\s*$/
    will do what you want.

    Once validated (as shown above), you can convert the list to an array as follows:
    @checked_ids = $INPUT{'checked_ids'} =~ /(\d+)/g

    If you really did need to check an array:

    my $bad; foreach (@array) { unless (/regexp/) { $bad = 1; last; } } die("Naughty\n") if $bad;

    or

    my $num_bad = grep { !/regexp/ } @array; die("$num_bad naughties\n") if $num_bad;
Re: Regex an Array?
by Forsaken (Friar) on May 02, 2005 at 17:42 UTC
    Directly from perlfunc:

    grep BLOCK LIST
    grep EXPR,LIST
    This is similar in spirit to, but not the same as, grep(1) and its relatives. In particular, it is not limited to using regular expressions.

    Evaluates the BLOCK or EXPR for each element of LIST (locally setting $_ to each element) and returns the list value consisting of those elements for which the expression evaluated to true. In scalar context, returns the number of times the expression was true.

    @foo = grep(!/^#/, @bar); # weed out comments

    or equivalently,

    @foo = grep {!/^#/} @bar; # weed out comments

    Note that $_ is an alias to the list value, so it can be used to modify the elements of the LIST. While this is useful and supported, it can cause bizarre results if the elements of LIST are not variables. Similarly, grep returns aliases into the original list, much as a for loop's index variable aliases the list elements. That is, modifying an element of a list returned by grep (for example, in a foreach, map or another grep) actually modifies the element in the original list. This is usually something to be avoided when writing clear code.

    See also map for a list composed of the results of the BLOCK or EXPR.

    An alternative would be to run a foreach loop on the array.

    If you want an answer more specific than that I fear you'll have to give a better example of what the data might look like.

    Remember rule one...

Re: Regex an Array?
by Fletch (Bishop) on May 02, 2005 at 17:40 UTC
    • you have a scalar (specifically a string), not an array
    • you've said what's valid, so check for the presence of anything that doesn't match (/[^\d\s,]/; \s because there's whitespace in your example)
    • alternately, go ahead and split on /,\s*/ and use grep to pick out (or exclude) the numbers (non-numbers)
      It's important to note that \d can match characters other than the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9, which is probably not desireable.
        \d can match characters other than the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9

        What other characters can it match?

Re: Regex an Array?
by davidrw (Prior) on May 02, 2005 at 17:49 UTC
    I think you want to lookup the grep() function to search the array. In this case to search for things that don't match -- maybe something like (not i'm assuming 'checked_ids' is an array ref -- if it's not, then have to either split() first or use a different regex check):
    if( grep( $_ !~ /^\d+$/, @{$INPUT{'checked_ids'}) ){ # you have bad data }
    Actually, re-reading the OP, i think your checked_ids is just a string. In that case,
    my @ids = split (/, +/, $INPUT{'checked_ids'}); # now check @ids w/grep() using the method above
    Or could check the string for containing just numbers and commas, but the above methods will be preferred (this is less robust and harder to read):
    if( $INPUT{'checked_ids'} !~ /^(\d+(, +)?)+$/ ){ # you have bad data }
    note: not the only, or even best, way to write this regex, but what it's trying to do is match just on set of digits with possibly a comma-space after it.