1

Regex PHP Code

// If url matches regex
            $regex = "/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/";
        if (preg_match($regex, $this->value)) {

            $this->valid();
        }

Error Message

Warning: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Unknown modifier 'p' in C:\Apache\www\profiletwist\lib\php\form\url.php on line 41
Call Stack
#   Time    Memory  Function    Location
1   0.0079  440016  {main}( )   ..\new.php:0
2   0.0964  667832  form->validate( )   ..\new.php:60
3   0.0968  668248  form_URL->validateUploadURL( )  ..\form.php:372
4   0.0969  668400  preg_match ( )  ..\url.php:41
Variables in local scope (#3)

$regex =



string '/^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$/' (length=79)

Question

How do I fix the regex for this "unknown modifier" error to not occur?

ultimately, I would like a regex that makes sure the text input matches:

"/upload/temporary/####_##_##_[A-z0-9 _-]+ "." [a-z]{3}

This is a filename target. The beginning does not change and the last part can be a random hash followed by an arbitrary extension. Further processing is done after the regex but this is the first test.

Thank you!

ProfileTwist
  • 1,524
  • 1
  • 13
  • 18
  • And there are maybe [+100 duplicates](http://stackoverflow.com/search?page=3&tab=newest&q=%5bphp%5d%20%5bregex%5d%20is%3aquestion%20closed%3ano%20unknown%20modifier). Basically you either use other delimiters or (the best practice) use [**`preg_quote()`**](http://php.net/preg-quote). – HamZa Jun 30 '13 at 21:22
  • @HamZa how is that best practice when you actually want to write a regular expression (and not just match a huge bunch of literal text)? I'd say picking other delimiters is best practice here. – Martin Ender Jun 30 '13 at 21:41
  • 1
    @ProfileTwist, since no one has included it in their answer, here is the official [reference on delimiters in PHP](http://www.php.net/manual/en/regexp.reference.delimiters.php). – Martin Ender Jun 30 '13 at 21:42
  • @m.buettner even if you don't know for sure what's the input ? – HamZa Jun 30 '13 at 21:43
  • 1
    @HamZa the `/upload/temporary/` part is not provided from some unknown source but a literal string in this case. the one who writes the code has full control over the contents of the regex here. hence, different delimiters. I'd only use `preg_quote` if I'm getting part of a regex from a string variable and I can't be sure that there are no metacharacters in that string. – Martin Ender Jun 30 '13 at 21:44

3 Answers3

7

In a regex string you have to escape your delimiters. Or better: use a character which doesn't appear in the regex itself as delimiter:

other delimiter (recommended):

$regex = "#^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$#";

escaped delimiters:

$regex = "/^(\/upload\/temporary\/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ \/-]+.[A-z]{2,4}$/";
bwoebi
  • 23,637
  • 5
  • 58
  • 79
3
$regex = "~^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[\w./-]+\.[a-z]{2,4}$~";

Change your delimiters to ~

When you use a delimiter for example /, you must escape all litteral / in your pattern otherwhise the regex engine believes that it is the end of the pattern.

Since u is a modifier and p isn't a modifier, you have this error because of the substring /^(/up....

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
3

You need to escape the front slashes or just use another delimiter (I've used ! in this case):

$regex = "!^(/upload/temporary/)[0-9]{4}_[0-9]{2}_[0-9]{2}_[A-Za-z0-9._ /-]+.[A-z]{2,4}$!"
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525