List of Special Characters in Regex in PHP
If you escape an ordinary character - nothing bad will happen - it will still represent itself. The exception is digits, they cannot be escaped.
Often there is doubt whether a given character is special. Some go so far as to escape all suspicious characters in a row. However, this is bad practice (clutters the regex with backslashes).
Are special characters: $ ^ . * + ? \
/ {} [] () |
Are not special characters: @ : , ' " ;
- _ = < > % # ~ ` & !
Given a string:
<?php
$str = 'a.a aba aea';
?>
Write a regex that will find the string
'a.a'
, without capturing the others.
Given a string:
<?php
$str = '2+3 223 2223';
?>
Write a regex that will find the string
'2+3'
, without capturing the others.
Given a string:
<?php
$str = '23 2+3 2++3 2+++3 345 567';
?>
Write a regex that will find the strings
'2+3'
, '2++3'
, '2+++3'
,
without capturing the others (+ can be any
number).
Given a string:
<?php
$str = '23 2+3 2++3 2+++3 445 677';
?>
Write a regex that will find the strings
'23'
, '2+3'
, '2++3'
,
'2+++3'
, without capturing the others.
Given a string:
<?php
$str = '*+ *q+ *qq+ *qqq+ *qqq qqq+';
?>
Write a regex that will find the strings
'*q+'
, '*qq+'
, '*qqq+'
,
without capturing the others.
Given a string:
<?php
$str = '[abc] {abc} abc (abc) [abc]';
?>
Write a regex that will find the strings
inside square brackets and replace them with '!'
.