preg_match doesn't behave as expected when validating windows filesystem path

Verbatim

I'm creating a regex that matches with Windows's filesystem file pathing:

c:\WINDOWS\SYSTEM32\cmd\a.php //valid match
c:\WINDOWS\SYSTEM32\cmd\a     //invalid match

with this pattern and code:

function validate_filepath()
{
    $view_path = 'c:\WINDOWS\SYSTEM32\cmd\a.php';

    // ^(?:[\w]\:)                  # matching drive
    //(?:\\[^\\:*?"<>|\r\n]+)+\\    # matching folder
    // [^\\:*?"<>|\r\n]+\.(?:php)   # matching file and php extension
    $regex = '/^(?:[\w]\:)(?:\\[^\\:*?"<>|\r\n]+)+\\[^\\:*?"<>|\r\n]+\.(?:php)/mi';

    preg_match($regex, $view_path, $matches);
    return($matches[0]);
}

I've tried the pattern using https://regex101.com and it matches with the path, but it doesn't when using PHP's preg_match. Where did I make a mistake?

I haven't really looked how PHP handles string, but I tested using double backslash and correct pattern in regexonline, but php still returns null $matches

EDIT: I'm using php-5.6.17

bwoebi

You'd need a quadruple backslash here: inside the single quotes the double backslash is already an escaped single backslash. [Try printing the $regex variable]

So:

$regex = '/^(?:[\w]\\:)(?:\\\\[^\\:*?"<>|\r\n]+)+\\\\[^\\\\:*?"<>|\r\n]+\\.(?:php)/mi';

Or alternatively use the here more readable nowdoc syntax:

$regex = <<<'REGEX'
/^(?:[\w]\:)(?:\\[^\\:*?"<>|\r\n]+)+\\[^\\:*?"<>|\r\n]+\.(?:php)/mi
REGEX;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

strpbrk doesn't behave as expected

From Dev

onActivityResult doesn't behave as expected

From Dev

PHP preg_match doesn't match

From Dev

PHP preg_match doesn't match

From Dev

SpriteKit - Edge doesn't behave as expected

From Dev

String example doesn't behave as expected

From Dev

Saving data to a file doesn't behave as expected

From Dev

RegExp in Javascript- (.*) doesn't behave as expected

From Dev

Split on pipe doesn't behave as expected

From Dev

.c_str() doesn't behave as expected

From Dev

Bootstrap 4 carousel Doesn't behave as expected

From Dev

Preg_match with Cyrillic doesn't work

From Dev

Preg_match with Cyrillic doesn't work

From Dev

Preg_match doesn't work right

From Dev

Why does preg_match doesn't find a string between tags when regex is OK?

From Dev

PHP preg_match doesn't match list separated with commas

From Dev

"Advanced" knockout css binding statement doesn't behave exactly as expected

From Dev

built-in predicate statistics in a Bratko example doesn't behave as expected

From Dev

Java HashSet remove(e) method doesn't behave as I expected

From Dev

Variable in generated JavaScript function doesn't behave as expected

From Dev

Spliterator generated by Iterables.partition() doesn't behave as expected?

From Dev

"Advanced" knockout css binding statement doesn't behave exactly as expected

From Dev

Assigning value to returned shared_ptr doesn't behave as expected

From Dev

JavaScript function call on JQuery .load() doesn't behave like expected

From Dev

Ubuntu two commands in one line doesn't behave as expected

From Dev

Why doesn't a case statement in BigQuery behave as expected?

From Dev

preg_match functions doesn't work properly

From Dev

preg_match doesn't work in a script php but work online

From Dev

Why doesn't this simple PHP preg_match() function work?

Related Related

  1. 1

    strpbrk doesn't behave as expected

  2. 2

    onActivityResult doesn't behave as expected

  3. 3

    PHP preg_match doesn't match

  4. 4

    PHP preg_match doesn't match

  5. 5

    SpriteKit - Edge doesn't behave as expected

  6. 6

    String example doesn't behave as expected

  7. 7

    Saving data to a file doesn't behave as expected

  8. 8

    RegExp in Javascript- (.*) doesn't behave as expected

  9. 9

    Split on pipe doesn't behave as expected

  10. 10

    .c_str() doesn't behave as expected

  11. 11

    Bootstrap 4 carousel Doesn't behave as expected

  12. 12

    Preg_match with Cyrillic doesn't work

  13. 13

    Preg_match with Cyrillic doesn't work

  14. 14

    Preg_match doesn't work right

  15. 15

    Why does preg_match doesn't find a string between tags when regex is OK?

  16. 16

    PHP preg_match doesn't match list separated with commas

  17. 17

    "Advanced" knockout css binding statement doesn't behave exactly as expected

  18. 18

    built-in predicate statistics in a Bratko example doesn't behave as expected

  19. 19

    Java HashSet remove(e) method doesn't behave as I expected

  20. 20

    Variable in generated JavaScript function doesn't behave as expected

  21. 21

    Spliterator generated by Iterables.partition() doesn't behave as expected?

  22. 22

    "Advanced" knockout css binding statement doesn't behave exactly as expected

  23. 23

    Assigning value to returned shared_ptr doesn't behave as expected

  24. 24

    JavaScript function call on JQuery .load() doesn't behave like expected

  25. 25

    Ubuntu two commands in one line doesn't behave as expected

  26. 26

    Why doesn't a case statement in BigQuery behave as expected?

  27. 27

    preg_match functions doesn't work properly

  28. 28

    preg_match doesn't work in a script php but work online

  29. 29

    Why doesn't this simple PHP preg_match() function work?

HotTag

Archive