regular expression false delimiter in wordpress

后端 未结 1 1685
忘了有多久
忘了有多久 2021-01-23 02:43

I am newbie to regular expression and I have this simple doubt.

I have found this code in wordpress

$self = preg_replace(\'|^.*/wp-admin/|i\', \'\', $se         


        
相关标签:
1条回答
  • 2021-01-23 03:30

    Have you tried it?

    From your link:

    When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

    So | is a perfectly valid delimiter. When you read the comments on that page, they suggest to not use meta characters (like |) as delimiters, when they should be used inside the regex.

    Since there is no alternation in your example $self = preg_replace('|^.*/wp-admin/|i', '', $self); there is no problem and it is working as expected.

    When you have an alternation in the regex (e.g. preg_match("|(F|f)oo|", "Foobar")) you will get a warning "Unknown modifier 'f'", because the interpreter thinks the regex does end at the first alternation.

    Conclusion: It's allowed, but not recommended to use regex meta-characters like |, ^, +, ... as regex delimiters.

    0 讨论(0)
提交回复
热议问题