The following Regular Expressions with PHP MCQs have been compiled by our experts through research, in order to test your knowledge of the subject of Regular Expressions with PHP. We encourage you to answer these 40 multiple-choice questions to assess your proficiency.
Please continue by scrolling down.
1:Which of the following flags is used to include a newline character \n while using the dot meta character of PHP Regular Expressions?
A. \z
B. \m
C. \s
D. \a
2:Which of the following PHP Regular Expression modifiers finds one or more occurrences of a specific character in a string?
A. ?
B. *
C. +
D. #
3:Analyze the following code snippet that uses the preg_last_error() PHP Regular Expression method:
<?php
preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar');
if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
echo 'Backtrack limit was exhausted!';
}
?>
What will be the output of the above code?
A. Backtrack limit was exhausted!
B. 0
C. 1
D. null
4:Suppose you use [hc]+at for pattern matching as a Regular Expression. Which of the following patterns does not match the above expression?
A. at
B. ccchat
C. hhat
5:What is the [\b] PHP Regular Expression modifier used for?
A. It is used as a word boundary specifying exactly what must be matched.
B. It is used to match only a single character in a string.
C. It is used to match a single backspace character.
D. It is used to match all alpha numeric characters.
6:Suppose you pass "^.{3}$" as a pattern to be matched to a specified string using PHP Regular Expressions. Which of the following strings match the above pattern?
A. .3$
B. 3$.$
C. 3_$.
D. $3_3$
7:Analyze the following code snippet that uses the preg_replace() PHP Regular Expression method:
<?php
$string = 'This is the {_FOO_} brought to you by {_BAR_}';
$template_vars=array("FOO" => "The PHP Way", "BAR" => "PHPro.orG");
$string = preg_replace("/{_(.*?)_}/ime", "\$template_vars['$1']",$string);
echo $string;
?>
What will be the output of the above code?
A. 0
B. This is the The FOO Way brought to you by BAR
C. 1
D. This is the The PHP Way brought to you by PHPro.orG
8:Analyze the following code:
<?php
$string = "1, 100 or 1000?";
preg_match_all("/10*/",$string,$matches);
foreach($matches[0] as $value)
{
echo $value;
}
?>
What will be the output of the above code snippet?
A. 1010
B. 110
C. 1,10
D. 1,100,1000
E. 11001000
9:Which of the following PHP Regular Expression modifiers finds zero or one occurrences of a specific character in a string?
What will be the output of the above code snippet?
A. 0
B. the
C. null
D. 1
11:Which of the following characters is found by a Regular Expression when the \s PHP modifier is set?
A. A newline character
B. A null character
C. A whitespace character
D. A special character
12:Which of the following PHP Regular Expression modifiers is used to make as few matches as possible in a given pattern?
A. \U
B. \S
C. \B
D. \W
13:Which of the following PHP Regular Expression modifier is used to find a pattern at the beginning of a string?
A. $
B. ^
C. ?=
D. \b
14:Suppose you perform pattern matching in a string using the \n PHP Regular Expression modifier. Which of the following values is returned if no newline character occurs in the string?
A. 0
B. null
C. false
D. None of the above
15:Which of the following is the correct syntax for creating a Regular Expression in PHP?
A. preg_match("/^ABC/i", $string)
B. preg_match("\^ABC\i", $string)
C. preg_match("^ABC", $string)
D. preg_match("/^ABC/i/", $string)
16:Which of the following characters is used to escape special characters in a given string during pattern matching using Regular Expressions?
A. /
B. \
C. $
D. #
17:Analyze the following code snippet that uses the preg_split() PHP Regular Expression method:
A. Array ( [0] => Andrew [1] => John [4] => Bill )
B.
Array ( [1] => Andrew [2] => John [3] => Bill )
C.
Array ( [0] => Andrew [1] => John [3] => Bill )
D.
Array ( [0] => Andrew )
20:Which of the following PHP Regular Expression modifiers is used to put the Regex on multiple lines while at the same time allowing comments within the Regular Expression itself?
A. x
B. u
C. e
D. s
21:Analyze the following code that uses the ?<= PHP Regular Expression modifier:
<?php
$string = 'I live in the whitehouse';
if(preg_match("/(?<!blue)house/i", $string))
{
echo 'Found a match';
}
else
{
echo 'No match found';
}
?>
What will be the output of the above code snippet?
A. Found a match
B. No match found
C. Compilation error
22:Analyze the following code:
<?php
$string = 'This is a [templateVar]';
preg_match_all("/[\[\]]/", $string, $matches);
foreach($matches[0] as $value)
{
echo $value;
}
?>
What will be the output of the above code snippet?
A. [\[\
B. [\]
C. []]
D. []
23:Analyze the following code that uses the {x} PHP Regular Expression modifier:
<?php
$string = "10 or 100 or 1000?";
preg_match_all("/\d{2}/", $string,$matches);
foreach($matches[0] as $value)
{
echo $value;
}
?>
What will be the output of the above code snippet?
A. 10
B. 101010
C. 1000
D. 10101000
24:Analyze the following code snippet that uses the preg_match() PHP Regular Expression method:
What will be the output of the above code snippet?
A. G,i,v,e,r,e,s,u,l,t
B. %_%
C. %%
D. G
35:Suppose you pass "world{2,3}" as a pattern to be matched to a specified string as shown in the code snippet below:
$pattern = "world{2,3}";
Which of the following strings does not match the above pattern?
A. worldd
B. world
C. worlddd
D. None of the above
36:Suppose you pass "a(bc)*" as a pattern to be matched to a specified string using PHP Regular Expressions. Which of the following strings does not match the above pattern?
A. aa
B. aade
C. abc
D. bcde
37:Which of the following PHP Regular Expression modifiers finds any non digit character in a given string?
A. \W
B. \S
C. \B
D. \D
38:Which of the following statements are true about the \b PHP Regular Expression modifier?
A. It is used to get a match only at the beginning of a word in a string.
B. It is used to get a match anywhere in a string.
C. If a match is not found using the \b modifier, it returns null.
D. It is used to get a match at the beginning or end of a word in a string.
39:Analyze the following code snippet that uses the preg_replace() PHP Regular Expression method: