1

I have the following regular expression tested at regex101 and it works there. However, when I try to use it inside the preg_match it doesn't work anymore. The problem is the slash: I tried escaping it but I can't get it working.

preg_match("/[<>\\\"'%;()&]/", "my\string");

The set of characters that I should match are:

< > \ " ' % ; ( ) &

1 Answer 1

4

Simply use a different delimiter:

preg_match("![<>\\\"'%;()&/]!", "my\string");
Sign up to request clarification or add additional context in comments.

2 Comments

thank you but what if I want to use the / as delimiter? I found that using 4 slashes to match a single \ works but I can't understand why I need that amount of characters
What is the point in using / as delimiter, if you want to use it as character? `` is a different question: First, you need to escape the backslash in the regular expression (-> 2 backslashes), then these two backslashes in PHP (-> 4 backslashes)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.