Рабочая тетрадь(4)
.pdf
(?:...)
Участок строки, который не должен попадать на выход регулярного выражения.
preg_match('/([A-Za-z ]+)(?:ith)/',
'John Smith',$matches);
$matches[0] => John Smith, [1] => John Sm
(?P<имя>...)
Определяет именованный вложенный шаблон.
preg_match('/(?P<century>[12][0-9]) (?P<year>[0-9]{2})/', 'PHP in 2005",$matches);
$matches[0] => 2005, [century] => 20, [1] => 20, [year] => 05, [2] => 05
'00:04:23:7с:5d:01'
'/^([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]$/'
'/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/'
$matches[0] => 00:04:23:7с:5d:01, [1] => 5d
'John Smith <jsmith@site.com>'
"/([^<]+)<([a-zA-Z0-9_-]+@([a-zA-Z0-9_-]+\\.)+
[a-zA-Z0-9_-]+)>/"
$matches[0] => John Smith <jsmith@site.com>,
[1]=> John Smith, [2] => jsmith@site.com,
[3]=> site.
\? \+ \* \[ \] \{ \}
'4**'
'/^4\*\*$/'
"/^4\\*\\*$/"
\\
'РНР\5';
'/^PHP\\\5$/';
"/^РНР\\\\5$/";
\t \n \f \r
ASCII 9, 10, 12, 13
\xhh
hex
\ddd
octal
\d
[0-9]
\D
[^0-9]
\s
[\t\n\f\r ]
\S
[^\t\n\f\r ]
\w
Любая буква, цифра, символ подчеркивания
\W
Противоположность \w
\b
Позиция между соседними символами \w и \W.
$string = "##Testing123##";
preg_match('/\b.+\b/', $string, $matches);
$matches[0] => Testing123
\B
Противоположность \b
'/\w+\s+\w+/' из 'John Smith'
<а href="http://php.net/">PHP</a> has an <a href="http://pnp.net/manual">good</a> manual.
'/<а.*>(.*)</а>/'
$matches[1] => good
"*" и "+" жадные квантификаторы
"?" - таблетка от жадности
'/<а.*?>(.*?)</а>/'
$matches[1] => PHP
i
/[a-z]/i = /[a-zA-Z]/
m
Многострочный поиск
"ABC\nDEF\nGHI";
'/^DEF/' соответствует $matches
'/^DEF/m' соответствует $matches[0] => DEF
S
Однострочный поиск. "." = . + перевод строки
"ABC\nDEF\nGHI";
'/BC.DE/' соответствует $matches
'/BC.DE/S' соответствует $matches[0] => BC\nDE
x
Пропуск пробелов и комментариев(#) в тексте шаблона
"ABC\nDEF\nGHI";
'/A B C/' - $matches
'/A B C/x' - $matches[0] => ABC
D
Что и $, если строка не заканчивается \n
"ABC\n"
'/BC$/' - $matches[0] => BC
'/BC$/D'
e
Только для preg_replace()
U
Ленивость по-умолчанию
'/<а.*?>(.*?)</а>/' = '/<а.*>(.*)</а>/U'
u
Включает режим кодировки символов UTF-8.
Метасимвол «.» может соответствовать многобайтовым символам
int preg_match ( string $pattern , string $subject
[, array $&matches [, int $flags [, int $offset ]]] )
$subject = "abcdef"; $pattern = '/^def/';
preg_match($pattern, $subject, $matches,
PREG_OFFSET_CAPTURE, 3);
▪Array ( )
preg_match($pattern, substr($subject,3),
$matches, PREG_OFFSET_CAPTURE);
▪ Array ( [0] => Array ( [0] => def [1] => 0 ) )
