ADVERTISEMENT
ADVERTISEMENT

PHP program to demonstrate the quantifiers in pattern to search a string using a regular expression

Here, we are going to demonstrate the quantifiers in pattern to search a string using a regular expression in PHP.
Submitted by Nidhi, on November 23, 2020

In this program, we will demonstrate the quantifiers in pattern to search a string using a regular expression, here we will use the preg_replace() function.

Program/Source Code:

The source code to demonstrate the quantifiers in pattern to search a string using regular expression is given below. The given program is compiled and executed successfully.

<?php
//PHP program to demonstrate the quantifiers in 
//pattern to search a string using a regular expression.

$str = "There are five bananas in the bucket";
$pattern = "/ba(na){2}/i";

if (preg_match($pattern, $str) == 1) 
    printf("Found");
else 
    printf("Not Found");
?>

Output:

Found

Explanation:

In the above program, we created a string variable $str, which is initialized with "There are five bananas in the bucket".

$pattern = "/ba(na){2}/i";

In the above code, we used quantifiers in pattern to search a string "banana" by looking for "ba" followed by two instances of "na".

if(preg_match($pattern, $str)==1)
	printf("Found");
else
	printf("Not Found");

In the above code, we checked the return value of the preg_match() function and then print the appropriate message on the webpage.

PHP Regular Expressions Programs »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.