PHP Strings & Patterns
PHP Regular Expression পর্ব-২
Last Updated on July 30, 2022 by Masud Alam

PHP Regular Expression দ্বিতীয় পর্বে আপনাকে স্বাগতম। এই পর্বে আমরা Regular Expressions এর Quantifier, Assertions এবং Sub Pattern Modifier কি এবং কিভাবে কাজ করে তার বিস্তারিত জানব। চলুন শুরু করা যাক :
PHP Regular Expression এ Quantifiers
String এর মধ্যে Pattern Matching করার সময় সেটা কতবার করবে আরো সহজ ভাবে বলা যায় repeated যেকোনো Matching এর জন্য PHP Regular Expression এ যেই special character গুলো ব্যবহৃত হয় , সে গুলোকে বলা হয় Quantifiers . নিম্নে Quantifier গুলোর list দেওয়া হলো :
https://w3programmers.com/bangla/php-course/
Quantifier Name | Description |
---|---|
n* | শুন্য অথবা একাধিক বার Search করার জন্য। |
n+ | এক অথবা একাধিক বার Search করার জন্য। |
n? | শূন্য (নেই) অথবা atleast একটি আছে কিনা তা Search করার জন্য |
{n} | exact number Search করার জন্য |
{n,} | সর্বনিম্ন সংখ্যক number Search করার জন্য |
{n,m} | দুটি সংখ্যার মধ্যবতী যেকোনো সংখ্যক number Search করার জন্য |
n* দিয়ে শুন্য অথবা একাধিক বার Search
<?php /*** 4 x and 4 z chars ***/ $string = "xxxxzzzz"; /*** greedy regex ***/ preg_match_all("(x*)",$string,$matches); /*** results ***/ print_r($matches); ?>
Output
Array ( [0] => Array ( [0] => xxxx [1] => [2] => [3] => [4] => [5] => ) )
n+ দিয়ে এক অথবা একাধিক বার Search করার জন্য।
<?php /*** 5 x and 3 z chars ***/ $string = "xxzxzzxx"; /*** greedy regex ***/ preg_match_all("(x+)",$string,$matches); /*** results ***/ print_r($matches); ?>
Output
Array ( [0] => Array ( [0] => xx [1] => x [2] => x ) )
n? দিয়ে atleast একটি আছে কিনা তা Search করার জন্য
<?php /*** 5 x and 3 z chars ***/ $string = "xxzxzzxx"; /*** greedy regex ***/ echo preg_match("(p+?)",$string,$matches)?"P is Found":"P is not found"; echo "<br>"; echo preg_match("(x+?)",$string,$matches)?"x is Found":"x is not found"; ?>
Output
P is not found x is Found
{n} দিয়ে exact number Search
<?php // create a string $string = 'PHP123'; // look for a match echo preg_match("/PHP[0-9]{3}/", $string, $matches)?"Yes, there are three Decimal Digits after PHP.":"No, there are not three Decimal Digits after PHP."; ?>
output
Yes, there are three Decimal Digits after PHP.
https://w3programmers.com/bangla/php-course/
{n,} দিয়ে সর্বনিম্ন সংখ্যক number Search
<?php // create a string $string = 'PHP123'; // look for a match echo preg_match("/PHP[0-9]{2,}/", $string, $matches)?"Yes, there are tow to more Decimal Digits after PHP.":"No, there are not Two to more Decimal Digits after PHP."; ?>
Output
Yes, there are tow to more Decimal Digits after PHP.
{n,m} দিয়ে দুটি সংখ্যার মধ্যবতী যেকোনো সংখ্যক number Search
<?php // create a string $string = 'PHP123'; // look for a match echo preg_match("/PHP[0-9]{2,3}/", $string, $matches)?"Yes, here there are numbers between two and five after PHP.":"No,There there are no numbers between two and five after PHP."; ?>
Output
Yes, here there are numbers between two and five after PHP.
PHP Regular Expression এ Pattern Modifiers
PHP Regular Expression এ প্যাটার্ন এ Search এর ধরণ পরিবর্তনের জন্য Forward Slash এর পর কিছু special Character যেমন : /i, /m ইত্যাদি ব্যবহৃত হয়। এগুলোকে বলা হয় Pattern Modifiers. নিম্নে সবগুলো Modifier এর list দেওয়া হলো :
Pattern Modifier Name | Description |
---|---|
i | Case Insensitive Search করবে। |
m | একাধিক লাইনে Search করার জন্য ব্যবহৃত হয়। |
x | comments এবং white space ও search এর অন্তর্গত করে। |
e | preg_replace এর বেলায় শুধু ব্যবহৃত হবে –evaluation replacement কে enable করে। |
U | Pattern কে ungreedy সার্চ এর জন্য ব্যবহার করে। |
u | Pattern কে UTF-8 বুঝানোর ব্যবহার হয়। |
/i দিয়ে Case Insensitive Search
<?php // create a string $string = 'abcdefghijklmnopqrstuvwxyz0123456789'; // try to match our pattern if(preg_match("/^ABC/i", $string)) { // echo this is it matches echo 'The string begins with abc'; } else { // if not match is found echo this line echo 'No match found'; } ?>
Output
The string begins with abc
লক্ষ্য করুন , আমাদের প্যাটার্নটি Uppercase এবং String টি lowercase হওয়ার পর ও /i ব্যবহারের ফলে Search এ কোনো সমস্যা হয় নাই।
https://w3programmers.com/bangla/php-course/
/m দিয়ে একাধিক লাইনে Search
<?php // create a string $string = 'Bangladesh'."\n".'India'."\n".'Pakistan'."\n".'Srilanka'."\n"; // look for a match if(preg_match("/^Pakistan/im", $string)) { echo 'Pattern Found'; } else { echo 'Pattern not found'; } ?>
Output
Pattern Found
তবে আপনি যদি প্যাটার্ন শেষে /m না দেন, Pattern Not Found রেজাল্ট আসবে। অর্থাৎ Pakistan শব্দটি new line এ হওয়ায় Pattern Matching হবেনা। নিচের উদাহরণ দেখুন :
<?php // create a string $string = 'Bangladesh'."\n".'India'."\n".'Pakistan'."\n".'Srilanka'."\n"; // look for a match if(preg_match("/^Pakistan/i", $string)) { echo 'Pattern Found'; } else { echo 'Pattern not found'; } ?>
Output
Pattern not found
/x দিয়ে comments এবং white space এর মধ্যে search
<?php // create a string $string = 'Bangladesh'."\n".'Pakistan'."\n".'India'."\n".'Nepal'."\n"; // create our regex using comments and store the regex // in a variable to be used with preg_match $regex =' / # opening double quote ^ # caret means beginning of the string India # the pattern to match /imx'; // look for a match if(preg_match($regex, $string)) { echo 'Pattern Found'; } else { echo 'Pattern not found'; } ?>
Output
Pattern Found
তবে আপনি যদি প্যাটার্ন শেষে /x না দেন, Pattern Not Found রেজাল্ট আসবে। অর্থাৎ India শব্দটি Comment এ হওয়ায় Pattern Matching হবেনা। নিচের উদাহরণ দেখুন :
<?php // create a string $string = 'Bangladesh'."\n".'Pakistan'."\n".'India'."\n".'Nepal'."\n"; // create our regex using comments and store the regex // in a variable to be used with preg_match $regex =' / # opening double quote ^ # caret means beginning of the string India # the pattern to match /im'; // look for a match if(preg_match($regex, $string)) { echo 'Pattern Found'; } else { echo 'Pattern not found'; } ?>
Output
Pattern not found
https://w3programmers.com/bangla/zend-certified-php-engineering-zcpe-course/
/U দিয়ে Pattern কে ungreedy সার্চ (অর্থাৎ atleast একটা Match করলেই হবে।) এই রকম search
<?php /*** a simple string ***/ $string = 'foobar foo--bar fubar'; /*** try to match the pattern ***/ if(preg_match("/foo(.*)bar/U", $string)){ echo 'Match found'; } else{ echo 'No match found'; } ?>
PHP Regular Expression এ Point Based Assertions
একটি String এর ঠিক কোন Point থেকে প্যাটার্ন টি ম্যাচিং শুরু করবে তা নির্ধারণের জন্য PHP তে assertion character গুলো ব্যবহৃত হয়। নিম্নে সবগুলো assertion এর list দেওয়া হলো :
Point based assertions Name | Description |
---|---|
\b | সম্পূর্ণ স্বতন্ত্র word হিসেবে search করার জন্য ব্যবহৃত হয়। |
\B | সম্পূর্ণ স্বতন্ত্র word না হয়ে word একটা অংশ হিসেবে search করার জন্য ব্যবহৃত হয়। |
\A | String এর শুরু থেকে Search করার জন্য ব্যবহৃত হয়। (independent of multiline mode) |
\Z | String এর শেষের দিক থেকে Search করার জন্য ব্যবহৃত হয়। বা নতুন লাইনের শেষে। (independent of multiline mode) |
\z | String এর শেষের দিক থেকে Search করার জন্য ব্যবহৃত হয়। (independent of multiline mode) |
\G | string এর মধ্যের word গুলোর প্রথম থেকে ম্যাচিং Serch.অর্থাৎ, word এর মাঝে ম্যাচ করবেনা। |
\b দিয়ে সম্পূর্ণ স্বতন্ত্র word হিসেবে search
<?php /*** a simple string ***/ $string = 'Masud is staying at the lab.'; /*** here we will try match the string "lab" ***/ if(preg_match ("/\blab\b/i", $string)) { /*** if we get a match ***/ echo "Lab is a completely separate word"; } else { /*** if no match is found ***/ echo 'There is no separate word named Lab'; } ?>
Output
Lab is a completely separate word
আবার যদি আমরা stay ওয়ার্ডকে সার্চ করি, তাহলে প্যাটার্ন Match করবেনা। কারণ stay word টি সম্পূর্ণ seperate কোনো word নয়। নিচের উদাহরণটি দেখুন:
<?php /*** a simple string ***/ $string = 'Masud is staying at the lab.'; /*** here we will try match the string "lab" ***/ if(preg_match ("/\bstay\b/i", $string)) { /*** if we get a match ***/ echo "stay is a completely separate word"; } else { /*** if no match is found ***/ echo 'There is no Completely separate word named stay'; } ?>
Output
There is no Completely separate word named stay
\B দিয়ে word word একটা অংশ হিসেবে search
<?php /*** a simple string ***/ $string = 'Masud will available at 6:00 PM'; /*** here we will try match the string "lab" ***/ if(preg_match ("/lab\B/i", $string)) { /*** if we get a match ***/ echo "Lab is a part of word"; } else { /*** if no match is found ***/ echo 'Lab is a completely Seperate Word'; } ?>
Output
Lab is a part of word
আবার will যেহেতু সম্পূর্ণ আলাদা word , তাই wil এর বেলায় pattern match করবেনা। নিচের উদাহরণ টি দেখুন :
<?php /*** a simple string ***/ $string = 'Masud will available at 6:00 PM'; /*** here we will try match the string "lab" ***/ if(preg_match ("/will\B/i", $string)) { /*** if we get a match ***/ echo "will is a part of word"; } else { /*** if no match is found ***/ echo 'will not a part of Word'; } ?>
Output
will not a part of Word
https://w3programmers.com/bangla/zend-certified-php-engineering-zcpe-course/
\A দিয়ে String এর শুরু থেকে Search
<?php // create a string $string = 'abcdefghijklmnopqrstuvwxyz0123456789'; // try to match our pattern if(preg_match("/\Aabc/i", $string)) { // echo this is it matches echo 'The string begins with abc'; } else { // if not match is found echo this line echo 'No match found'; } ?>
Output
The string begins with abc
\z দিয়ে String এর শেষের দিক থেকে Search
<?php // create a string $string = 'abcdefghijklmnopqrstuvwxyz0123456789'; // try to match our pattern if(preg_match("/\89\z/i", $string)) { // echo this is it matches echo 'The string ends with 89'; } else { // if not match is found echo this line echo 'No match found'; } ?>
Output
The string ends with 89
\G string এর মধ্যের word গুলোর প্রথম থেকে ম্যাচিং
<?php $pattern = '#(match),#'; $subject = "match,match,match,match,not-match,match"; preg_match_all( $pattern, $subject, $matches ); //Will output match 5 times because it skips over not-match print_r($matches[0]); $pattern = '#(\Gmatch),#'; $subject = "match,match,match,match,not-match,match"; preg_match_all( $pattern, $subject, $matches ); //Will only output match 4 times because at not-match the chain is broken print_r($matches[0]); ?>
Output
Array ( [0] => match, [1] => match, [2] => match, [3] => match, [4] => match, ) Array ( [0] => match, [1] => match, [2] => match, [3] => match, )
https://w3programmers.com/bangla/php-course/
Subpattern Modifiers এবং Assertions
Modifier Name | Description |
---|---|
(?=) | দুটি word এর প্রথম অংশ এর সাথে দ্বিতীয় অংশ যুক্ত কিনা তা check/Match করার জন্য ব্যবহৃত হয়। |
(?!) | দুটি word এর প্রথম অংশ এর সাথে দ্বিতীয় অংশ যুক্ত নয় তা check/Match করার জন্য ব্যবহৃত হয়। |
(?<=) | দুটি যুক্ত word এর নির্দিষ্ট একটি word অন্য আরেকটি word এর আগে কিনা তা check করার জন্য ব্যবহৃত হয়। |
( ? < ! ) | দুটি যুক্ত word এর নির্দিষ্ট একটি word অন্য আরেকটি word এর আগে নয়, তা check করার জন্য ব্যবহৃত হয়। |
(?=) দিয়ে দুটি যুক্ত word এর প্রথম অংশ এর সাথে দ্বিতীয় অংশকে check/Match
<?php /*** a simple string ***/ $string = 'I live in the whitehouse'; /*** try to match white followed by house ***/ if(preg_match("/white(?=house)/i", $string)) { /*** if we find the word white, followed by house ***/ echo 'Found a match'; } else { /*** if no match is found ***/ echo 'No match found'; } ?>
Output
Found a match
(?!) দিয়ে দুটি word এর প্রথম অংশ এর সাথে দ্বিতীয় অংশ যুক্ত নয় তা check/Match করা
<?php /*** a simple string ***/ $string = 'I live in the white house'; /*** try to match white not followed by house ***/ if(preg_match("/white(?!house)/i", $string)) { /*** if we find the word white, not followed by house ***/ echo 'Found a match'; } else { /*** if no match is found ***/ echo 'No match found'; } ?>
Output
Found a match
(?<=) দিয়ে দুটি যুক্ত word এর নির্দিষ্ট একটি word অন্য আরেকটি word এর আগে কিনা তা check করা
<?php /*** a simple string ***/ $string = 'I live in the whitehouse'; /*** try to match house preceded by white ***/ if(preg_match("/(?<=white)house/i", $string)) { /*** if we find the word white, not followed by house ***/ echo 'Found a match'; } else { /*** if no match is found ***/ echo 'No match found'; } ?>
Output
Found a match
https://w3programmers.com/bangla/zend-certified-php-engineering-zcpe-course/
দুটি যুক্ত word এর নির্দিষ্ট একটি word অন্য আরেকটি word এর আগে নয়, তা check করা।
<?php /*** a simple string ***/ $string = 'I live in the white house'; /*** try to match house preceded by white ***/ if(preg_match("/(?<!white)house/i", $string)) { /*** if we find the word white, not followed by house ***/ echo 'Found a match'; } else { /*** if no match is found ***/ echo 'No match found'; } ?>
Output
Found a match