Solved

PHP regular expression

Posted on 2014-11-17
7
205 Views
Last Modified: 2014-11-17
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.
0
Comment
Question by:gplana
7 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40448127
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.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40448175
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).
0
 
LVL 15

Author Comment

by:gplana
ID: 40448279
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.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 15

Author Comment

by:gplana
ID: 40448281
I'm leaving third and fourth parameters empty. Is that ok?
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40448385
You'll need to include start of string ( ^ ) and end of string ( $ ) anchors, then:

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

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40448388
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

0
 
LVL 15

Author Closing Comment

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

Regards.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question