0

In regular expressions we can escape special characters like: /\. - dot/

And what if I want to "escape" a substring: /Some **esc.{2}aped** phrase/ (to match this pattern "Some esc.{2}aped phrase") ? Is there any such character sequences (to replace **)?

PHP example. I have pattern

$pattern = "/Some unknown text {$here}: (\\d+)/";

with UTF8 string $here. And want to test UTF8 string $input with $here substituted "as is" (ignore special characters in $here):

preg_match( $pattern, $input, $matches );

Thanks

1 Answer 1

1

If I'm reading this right, you want to embed an unknown string into a reqular expression.

You can use preg_quote() for this:

$unknown = "Some. Text";
$regex = '/Some unknown text '.preg_quote($unknown, '/').': (\d+)/u';

Also, for UTF-8 encoded regular expressions, you might want to use the u modifier to recognize UTF-8 character sequences.

Sign up to request clarification or add additional context in comments.

Comments

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.