Character Groups in PHP Regular Expressions
There are special commands that allow you to
select entire groups of characters at once. The command
\d means a digit from 0 to 9.
The command \w denotes a digit, Latin
letter, or underscore. The command \s
denotes a space or whitespace character:
space, newline, tab. You can
invert the meaning of the command by writing a capital
letter: for example, if \d is a digit,
then \D is a non-digit.
Example
Let's find all digits:
<?php
$str = '1 12 123';
$res = preg_replace('#\d#', '!', $str);
?>
As a result, the following will be written to the variable:
'! !! !!!'
Example
Repetition operators consider group commands
as a whole, meaning grouping parentheses
are not needed. In the next example, the search pattern
looks like this: digit from 0 to 9
one or more times:
<?php
$str = '1 12 123 abc @@@';
$res = preg_replace('#\d+#', '!', $str);
?>
As a result, the following will be written to the variable:
'! ! ! abc @@@'
Example
In the next example, the search pattern looks
like this: anything one or more times,
but not a digit from 0 to 9:
<?php
$str = '123abc3@@';
$res = preg_replace('#\D+#', '!', $str);
?>
As a result, the following will be written to the variable:
'123!3!'
Example
In this example, the search pattern looks like this: whitespace character once:
<?php
$str = '1 12 123 abc @@@';
$res = preg_replace('#\s#', '!', $str);
?>
As a result, the following will be written to the variable:
'1!12!123!abc!@@@'
Example
In this example, the search pattern looks like this:
NON-whitespace character one or more times.
All substrings separated by spaces will be replaced
by '!':
<?php
$str = '1 12 123 abc @@@';
$res = preg_replace('#\S+#', '!', $str);
?>
As a result, the following will be written to the variable:
'! ! ! ! !'
Example
In this example, the search pattern looks like this:
digit or letter one or more times.
All substrings consisting of digits and letters
will be replaced by '!':
<?php
$str = '1 12 123a Abc @@@';
$res = preg_replace('#\w+#', '!', $str);
?>
As a result, the following will be written to the variable:
'! ! ! ! @@@'
Example
In this example, the search pattern looks like this:
NOT a digit and NOT a letter one or more times.
In our case, this definition includes
'@@@' and all spaces (they are also
not digits and not letters). Pay attention to
the fact that at the end there is one '!' - it
was transformed from the string ' @@@' - with
a space in front:
$str = '1 12 123 Abc @@@';
$res = preg_replace('#\W+#', '!', $str);
As a result, the following will be written to the variable:
'1!12!123!Abc!'
Practical Tasks
Given a string:
<?php
$str = 'a1a a2a a3a a4a a5a aba aca';
?>
Write a regex that will find strings
in which the letters 'a' are on the edges,
and between them is one digit.
Given a string:
<?php
$str = 'a1a a22a a333a a4444a a55555a aba aca';
?>
Write a regex that will find strings
in which the letters 'a' are on the edges,
and between them is any number of digits.
Given a string:
<?php
$str = 'aa a1a a22a a333a a4444a a55555a aba aca';
?>
Write a regex that will find strings
in which the letters 'a' are on the edges,
and between them is any number of digits (including
zero digits, i.e., the string 'aa').
Given a string:
<?php
$str = 'avb a1b a2b a3b a4b a5b abb acb';
?>
Write a regex that will find strings
of the following form: the letters
'a' and 'b' are on the edges,
and between them - not a number.
Given a string:
<?php
$str = 'ave a#b a2b a$b a4b a5b a-b acb';
?>
Write a regex that will find strings
of the following form: the letters
'a' and 'b' are on the edges,
and between them - not a letter and not a digit.
Given a string:
<?php
$str = 'ave a#a a2a a$a a4a a5a a-a aca';
?>
Write a regex that will replace all spaces
with '!'.