PHP Regular Expression পর্ব-২

PHP Regular Expressions
PHP Regular Expressions

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 দেওয়া হলো :

Zend Certified PHP Engineering (ZCPE) Course

PHP Regular Expression এ Quantifiers
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.

Zend Certified PHP Engineering (ZCPE) 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 দেওয়া হলো :

PHP Regular Expression এ Pattern Modifiers
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 এ কোনো সমস্যা হয় নাই।

Zend Certified PHP Engineering (ZCPE) 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

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 দেওয়া হলো :

PHP Regular Expression এ Point Based Assertions
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

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,
)

Zend Certified PHP Engineering (ZCPE) Course

Subpattern Modifiers এবং Assertions

PHP Regular Expression এ Sub Pattern এবং 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

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

আমি মাসুদ আলম, বাংলাদেশের ৩৬ তম Zend Certified Engineer । ২০০৯ সালে কম্পিউটার সাইন্স থেকে বেচেলর ডিগ্রী অর্জন করি। দীর্ঘ ১৫ বছর আমি Winux Soft, SSL Wireless, IBCS-PRIMAX, Max Group, Canadian International Development Agency (CIDA), Care Bangladesh, World Vision, Hellen Keller, Amarbebsha Ltd সহ বিভিন্ন দেশি বিদেশী কোম্পানিতে ডেটা সাইন্স, মেশিন লার্নিং, বিগ ডেটা, ওয়েব ডেভেলপমেন্ট এবং সফটওয়্যার ডেভেলপমেন্ট এর উপর বিভিন্ন লিডিং পজিশন এ চাকরি এবং প্রজেক্ট লিড করি। এছাড়াও বাংলাদেশের ১৮৫ জন জেন্ড সার্টিফাইড ইঞ্জিনিয়ার এর মধ্যে ১২০ এরও অধিক ছাত্র আমার হাতে জেন্ড সার্টিফাইড ইঞ্জিনিয়ার হয়েছেন। বর্তমানে w3programmers ট্রেনিং ইনস্টিটিউট এ PHP এর উপর Professional এবং Advance Zend Certified PHP -8.2 Engineering, Laravel Mastering Course with ReactJS, Python Beginning To Advance with Blockchain, Machine Learning and Data Science, Professional WordPress Plugin Development Beginning to Advance কোর্স করাই। আর অবসর সময়ে w3programmers.com এ ওয়েব টেকনোলজি নিয়ে লেখালেখি করি।

Leave a Reply