preg_match()Unknown modifier '*' warning

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
james2000
Forum Newbie
Posts: 2
Joined: Sun Dec 27, 2009 7:10 pm

preg_match()Unknown modifier '*' warning

Post by james2000 »

Hi guys,

I am a beginner using Regex. Since eregi has been DEPRECATED, I want to use preg_match. I 've got the following errors. Any recommendation is highly appreciated.

Warning: preg_match() [function.preg-match]: Unknown modifier '*' in


if ( preg_match('[0-9]*\.*[0-9]*', $version_number, $m ) )
{
$math_version_number = $m[0];

}
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: preg_match()Unknown modifier '*' warning

Post by ridgerunner »

Your problem is simple. The first parameter passed to preg_match() (the regex pattern), must have a regex delimiter in addition to the quotes - and yours does not have the required regex delimiters: (i.e. '/pattern/' is good, 'pattern' is bad.) You can use pretty much any non-alphanumeric character as a delimiter, but if that character appears anywhere in your regex, then you must escape it with a backslash. You can also use matching braces as regex delimiters. In fact in your example, preg_match was interpreting the square brackets as the regex delimiter, and the * following the last closing bracket was (erroneously) being interpreted as a regex modifier; and thus the error message says: "Unknown modifier"

To fix your function, simply wrap your pattern with forward slashes (a common delimiter choice) like so:

Code: Select all

if ( preg_match('/[0-9]*\.*[0-9]*/', $version_number, $m ) )
        {
            $math_version_number = $m[0]; 
            
        }
The PHP documentation help files have some good examples that show the proper syntax for all the preg_*() family of functions.

Cheers!
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: preg_match()Unknown modifier '*' warning

Post by ridgerunner »

Another thing... I assume you are looking to match versions like: 1.0, 2.2, 3.34 etc. Unfortunately, you have used the star * quantifier on everything in your regex which matches zero or more of the preceding term. Thus, in your regex, everything is optional and it will thus match an empty string! It will also match one or more dots in a row with no digits and other non-version-like strings.

Here is a regex that matches two or more sets of digits separated by a dot (i.e. 1.0, 1.2.3, 1.22.333.4444 etc):

Code: Select all

// long version of regex with comments to describe logic
$re_long = '/
  \b       # start of version should be on a word boundary
  \d+      # version must start with one or more digits
  (?:      # begin a non-capturing group to apply plus quantifier
    \.\d+  # match a dot followed by one or more digits
  )+       # one or more dot+digits must follow initial digits
  \b       # end of version should be on a word boundary
  /x';
 
// short version of same regex
$re_short = '/\b\d+(?:\.\d+)+\b/';
 
if (preg_match($re_long, $subject)) {
    # Successful match
} else {
    # Match attempt failed
}
Note that the $re_long regex is written using the 'x' "free-spacing" modifier which allows adding extra whitespace and comments which follow the pound # symbol. In this way you can comment your regex to allow others to more easily understand the logic. This regex also makes use of the '\d' metacharacter which is shorthand notation for the '[0-9]' digit character class. It also makes use of the '\b' metacharacter which forces the regex to match on a word boundary.

Regexes are fun! The www.regular-expressions.info website has some great info covering regular expressions. I highly recommend taking a little time to read the Quick Start (and if you are still interested, the more in-depth Regular Expression Tutorial). The time you spend will pay for itself many times over!

Hope this helps! :)
james2000
Forum Newbie
Posts: 2
Joined: Sun Dec 27, 2009 7:10 pm

Re: preg_match()Unknown modifier '*' warning

Post by james2000 »

Big thanks. Nice place to pop question and get answer. I have learned a lot by everyday visiting. Thanks.
Post Reply