Viewed   216 times

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:Apachewwwprofiletwistlibphpformurl.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!

 Answers

1

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}$/";
Sunday, October 9, 2022
 
kasor
 
5

You'd have to switch your machine's system locale to English, Control Panel + Language.

That's a rather impactful change since it also changes the locale for every other program on your machine. One thing you can try (but I cannot verify) is to whack the localization file that the C# compiler uses for strings. On your machine it should be located in c:windowsmicrosoft.netframeworkv4.0.303191036cscui.dll. Rename the file so the C# compiler can't find it and is forced to fallback to, hopefully, English. Btw, I guessed at 1036, there are lots of French locales. Locale IDs are listed here.

Thursday, October 27, 2022
1

Switching to preg_match_all was correct, now all you need to do is remove the 'g' from your regex:

$regexp = '/(productimages/)(w*)(/v/)(w*)(.jpg)/';
Sunday, August 14, 2022
 
4

There is no way to suppress an error other than to fix it.

An error, by its nature, is indicating that the compiler believes it cannot generate valid code. The only way to suppress errors is to fix them. Just add the return statement it wants and then raise an issue on Microsoft Connect indicating that you believe the compiler is getting this one wrong.

I suspect, however, that this is expected behaviour as the compiler is not aware that the method you are calling will always throw and to determine that in a predictable manner for any possible call tree would be difficult, if not impossible (imagine if you called a chain of 20 methods before concluding with a throw).

Tuesday, October 25, 2022
5

Your regex will break if the string $id_base has a / in it as you are using / as the regex delimiter.

To fix this use preg_quote on $id_base as:

if (preg_match('/'. preg_quote($id_base,'/').'-([0-9]+)$/', .....) {
Saturday, October 29, 2022
 
shilly
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :