Link to home
Start Free TrialLog in
Avatar of gplana
gplanaFlag for Spain

asked on

PHP regular expression

Hi.

I'm trying to check if a string has a list of numbers separed by commas. Any element separed by comma could be also an asterisk (*) instead of a number.

Here some examples of valid expressions:
123,456,23
123,*,23
345
*
*,123,*,456

And some non-valid expressions:
*,a,*
*,123,

I'm trying this regular expression:
/[0-9|\*](,([0-9]|\*)*/

I tested it on this page: https://es.functions-online.com/preg_match.html
however it seems it always return 1. Isn't preg_match the correct function to call for this purpose? What I'm doing wrong?

Thank you.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

REGEX always drives me nuts.  I figure it out for the thing I'm working on and then I forget most of it.  So the next time, I go back to this page http://php.net/manual/en/function.preg-match.php and figure it out again.  Check out the PCRE link at the bottom of the page also.
Try:

([0-9]+|\*)(,([0-9]+|\*))*

Open in new window


It appears that only the first part of your pattern is broken. You're trying to put the alternation inside of the character class (i.e. the square brackets).
Avatar of gplana

ASKER

Thanks kaufmed, but I tryied your expression (just surrounding it by / as delimiter character) on the link https://es.functions-online.com/preg_match.html and trying with this text "*,121,a" return a result of 1. It seems like it returns a result of 1 always.
Avatar of gplana

ASKER

I'm leaving third and fourth parameters empty. Is that ok?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sometimes it's easier to get it all right if you break it down into components and not try to do it all with a single regular expression.  Here's my take on the problem.
http://iconoun.com/demo/temp_gplana.php

<?php // demo/temp_glpana.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28564006.html

// TEST CASES
$tests = array
( '123,456,23'
, '123,*,23'
. '345'
, '*'
, '*,123,*,456'

, '*,a,*'
, '*,123,'
)
;

// RUN THE TEST CASES
foreach ($tests as $test)
{
    echo PHP_EOL . htmlentities($test);
    if (valid_substrings($test)) echo ' VALID';
        else echo ' INVALID';
}


// A FUNCTION TO TEST THE SUBSTRINGS
function valid_substrings($str)
{
    // A REGULAR EXPRESSION TO DISALLOW ANYTHING BUT NUMBERS OR THE ASTERISK
    $rgx
    = '#'       // REGEX DELIMITER
    . '^'       // AT START OF STRING
    . '['       // START CHARACTER CLASS
    . '^'       // NEGATION - MATCH ANYTHING NOT THESE CHARACTERS
    . '0-9'     // NUMBERS
    . '\*'      // ESCAPED ASTERISK
    . ']'       // ENDOF CHARACTER CLASS
    . '$'       // AT ENDOF STRING
    . '#'       // REGEX DELIMITER
    ;

    // BREAK ON COMMAS AND TEST EACH SUBSTRING
    $subs = explode(',', $str);
    foreach ($subs as $sub)
    {
        if (empty($sub))            return FALSE;
        if (preg_match($rgx, $sub)) return FALSE;
    }
    return TRUE;
}

Open in new window

Avatar of gplana

ASKER

Excellent. This was my problem: I needed an start of string and an end of string. Thank you for teaching me in that.

Regards.