Solved

PHP regular expression

Posted on 2014-11-17
7
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
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 110

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

630 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