Ekspresi reguler adalah alat yang sangat kuat, tetapi kebijaksanaan konvensional adalah bahwa sekali ditulis, mereka sangat sulit untuk dipahami, jadi mempertahankannya bukanlah pengalaman yang menyenangkan. Dikumpulkan di sini adalah tips untuk membantu membuat mereka lebih mudah dibaca.
PHP PCRE โ PHP 7.3, PCRE2 โ . PHP , , . PHP , ctype*, URL-, โ . IDE , , , .
, , . . , - PHP ( PHP 7.3). , . , PHP, JavaScript , ES2018.
:
-;
;
;
;
;
.
-
โ . -, โ . :
/(foo|bar)/i
(foo|bar)
โ , i
โ , , /
โ . /
, . , ~, !, @, #, $
. , , \
โ . : {}, (), [], <>
, . , , , . - , . , , . (, ^, $,
, ), . , , , . , /
, โ , , URL-. :
preg_match('/^https:\/\/example.com\/path/i', $uri);
โ#โ, , :
preg_match('#^https://example.com/path#i', $uri);
- . . , .
, *
, +
, $
. , /Username: @[a-z\.0-9]/
โ.โ , .
, , . , -
. , , , , .
, /[A-Z]/
, A Z. (/[A\-Z]/)
, โ , A, Z . , , , . , /[AZ-]/
, /[A\-Z]/
, .
( , ), . , :
/Price: [0-9\-\$\.\+]+/
/Price: [0-9$.+-]+/
X
, , , . , , , , . โ :
preg_match('/x\yz/X', ''); // "y" โ , โ
:
Warning: preg_match(): Compilation failed: unrecognized character follows \ at offset 2 in ... on line ...
, ()
, , , , , , .
, โPrice: โฌ24
โ.
$pattern = '/Price: (ยฃ|โฌ)(\d+)/';
$text = 'Price: โฌ24';
preg_match($pattern, $text, $matches);
2 , , ((ยฃ|โฌ))
, โ . , $matches
, , :
var_dump($matches);
array(3) {
[0]=> string(12) "Price: โฌ24"
[1]=> string(3) "โฌ"
[2]=> string(2) "24"
}
, . , , ?:
. , , . , , (ยฃ|โฌ)
, , : (?:ยฃ|โฌ)
.
$pattern = '/Price: (?:ยฃ|โฌ)(\d+)/';
$text = 'Price: โฌ24';
preg_match($pattern, $text, $matches);
var_dump($matches);
$matches
1 โ :
array(2) {
[0]=> string(12) "Price: โฌ24"
[1]=> string(2) "24"
}
, , , , , .
, . , , ,
, , :
/Price: (?<currency>ยฃ|โฌ)(?<price>\d+)/
, (?
, , . , (?<currency>ยฃ|โฌ)
โ currency, (?<price>\d+)
โ price. , , โ . , :
$pattern = '/Price: (?<currency>ยฃ|โฌ)(?<price>\d+)/';
$text = 'Price: โฌ24';
preg_match($pattern, $text, $matches);
var_dump($matches);
:
array(5) {
[0]=> string(12) "Price: โฌ24"
["currency"]=> string(3) "โฌ"
[1]=> string(3) "โฌ"
["price"]=> string(2) "24"
[2]=> string(2) "24"
}
, $matches
, , .
, , ["currency"]=> "โฌ"
, [1]=> "โฌ"
.
PHP , :
Warning: preg_match(): Compilation failed: two named subpatterns have the same name (PCRE2_DUPNAMES not set) at offset ... in ... on line ....
/Price: (?<currency>ยฃ|โฌ)?(?<price>\d+)(?<currency>ยฃ|โฌ)?/J
2 currency, , J
. , currency , . , , :
$pattern = '/Price: (?<currency>ยฃ|โฌ)?(?<price>\d+)(?<currency>ยฃ|โฌ)?/J';
$text = 'Price: โฌ24ยฃ';
preg_match($pattern, $text, $matches);
var_dump($matches);
array(6) {
[0]=> string(14) "Price: โฌ24ยฃ"
["currency"]=> string(2) "ยฃ"
[1]=> string(3) "โฌ"
["price"]=> string(2) "24"
[2]=> string(2) "24"
[3]=> string(2) "ยฃ"
}
, . , , PHP-, , . โ . .
, :
$pattern = '/Price: (?<currency>ยฃ|โฌ)(?<price>\d+)/i';
:
$pattern = '/Price: ';
$pattern .= '(?<currency>ยฃ|โฌ)'; // Capture currency symbols ยฃ or โฌ
$pattern .= '(?<price>\d+)'; // Capture price without decimals.
$pattern .= '/i'; // Flags: Case-insensitive
. x
, , . , . :
/Price: (?<currency>ยฃ|โฌ)(?<price>\d+)/i
/Price: \s (?<currency>ยฃ|โฌ) (?<price>\d+) /ix
, , x
. , , , . , , \s
.
x
, #
, PHP . , . , :
/Price: (?<currency>ยฃ|โฌ)(?<price>\d+)/i
:
/Price: # Check for the label "Price:"
\s # Ensure a white-space after.
(?<currency>ยฃ|โฌ) # Capture currency symbols ยฃ or โฌ
(?<price>\d+) # Capture price without decimals.
/ix
PHP, Heredoc Nowdoc . , :
$pattern = <<<PATTERN
/Price: # Check for the label "Price:"
\s # Ensure a white-space after.
(?<currency>ยฃ|โฌ) # Capture currency symbols ยฃ or โฌ
(?<price>\d+) # Capture price without decimals.
/ix # Flags: Case-insensitive
PATTERN;
preg_match($pattern, 'Price: ยฃ42', $matches);
, , , . , โ \d
, , [0-9]
. \D
, โ , [^0-9]
. , , , , , :
/Number: [0-9][^0-9]/
:
/Number: \d\D/
\w
โ , ,[A-Za-z0-9_]
,
/[A-Za-z0-9_]/
:
/\w/
[:xdigit:]
โ ,[A-Fa-f0-9]
,
/[a-zA-F0-9]/
:
/[[:xdigit:]]/
\s
โ ,[ \t\r\n\v\f]
,
/ \t\r\n\v\f/
/\s/
/u
, , . \p{_}
, _
โ . \p
"p" , \P{FOO}
, โ , . , , , \p{Sc}
, , , , , . , : \p{Currency_Symbol}
, PHP.
:
$pattern = '/Price: \p{Sc}\d+/u';
:
$text = 'Price: ยฅ42';
, . , , , . , . , \p{Sinhala}
, \x{0D80}-\x{0DFF
}. , :
$pattern = '/[\x{0D80}-\x{0DFF}]/u';
, :
$pattern = '/\p{Sinhala}/u';
,
$text = 'เถดเทเถเถ เทเถดเท.เทเทเถ เท`;
$contains_sinhala = preg_match($pattern, $text);
, , , !
P.S. โ - . , .