Cara meningkatkan keterbacaan ekspresi reguler di PHP

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 ....
      
      



, J



(UPD: , PHP 7.2.0, ?J



):





/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. โ€” - . , .








All Articles