Regular Expressions with PHP Fiverr Test
Suppose you use [hc]+at for pattern matching as a Regular Expression. Which of the following patterns does not match the above expression?
at
ccchat
hhat
Analyze the following code snippet that uses the preg_split() PHP Regular Expression method:
<?php
$keywords = preg_split("/[\s,]+/", "php, regular expressions");
print_r( $keywords )
?>
What will be the output of the above code?
Array ( [0] => php [1] => regular [2] => expressions )
Array ( [1] => php [2] => regular [3] => expressions )
Array ( [0]- php [1] - regular [2]- expressions )
Array ( [0] => php/ [1] => regular/ [2] => expressions )
Which of the following flags is used to include a newline character \n while using the dot meta character of PHP Regular Expressions?
\z
\m
\s
\a
Which of the following PHP Regular Expression modifiers finds zero or one occurrences of a specific character in a string ?
?
*
+
#
Which of the following examples use the PHP positive lookahead assertion in a correct format?
preg_match("/eg?=:/", $string);
preg_match("/eg+(?=:)/", $string, $match)
preg_match("/eg+?=:/", $string, $match)
preg_match("/eg+(?=:)/", $string)
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 ?
3$
3$.$
3_$.
$3_3$
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?
x
u
e
s
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?
aa
aade
abc
bcde
Which of the following PHP Regular Expression modifiers is used to find a pattern at the end of a string ?
$
\z
^
s
What is the [\b] PHP Regular Expression modifier used for?
It is used as a word boundary specifying exactly what must be matched.
It is used to match only a single character in a string.
It is used to match a single backspace character.
It is used to match all alpha numeric characters.
Which of the following PHP Regular Expression modifier is used to find a pattern at the beginning of a string ?
$
^
?=
b
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 ?
0
null
false
None of the above
Which of the following characters can be used as a delimiter in a PHP Regular Expression ?
~
*
<
@
Which of the following PHP Regular Expression modifiers is used to make as few matches as possible in a given patterm ?
\U
\S
\B
W
Which of the following characters is found by a Regular Expression when the \s PHP modifier is set?
A newline character
A null character
A whitespace character
A special character
Which of the following is the correct syntax for creating a Regular Expression object in PHP?
$string = 'abcd';
echo preg_match("abc", $string);
$string = "abcd";
echo preg_match(abc/$string);
$string = 'abcd';
echo preg_match("/abc/", $string);
$string = 'abcd';
echo preg_match(/abc/$string);
Which of the following characters is used to escape special characters in a given string during pattern matching using Regular Expressions?
/
\
$
#
Which of the following characters is not matched by a Regular Expression when a .(dot) character is set ?
Any special character
null character
white space character
newline character
Which of the following is the correct syntax for creating a Regular Expression in PHP ?
preg_match("/^ABC/i", $string)
preg_match("\^ABC\i", $string)
preg_match("^ABC", $string)
preg_match("/^ABC/i/", $string)
Which of the following PHP Regular Expression modifiers finds any non digit character in a given string ?
\W
\S
\B
D
Which of the following statements are true about the \b PHP Regular Expression modifier?
it is used to get a match only at the beginning of a word in a string
It is used to get a match anywhere in a string
If a match is not found using the \b modifier, it returns null.
It is used to get a match at the beginning or end of a word in a string
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?
Found a match
No match found
Compilation error
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?
1010
110
1,10
1,100,1000
11001000
Analyze the following code snippet that uses the preg_grep() PHP Regular Expression method:
<?php
$names = array('Andrew','John','Peter','Nastin','Bill');
$output = preg_grep('/^[a-m]/i', $names);
print_r( $output );
?>
What will be the output of the above code ?
Array([0] => Andrew[1] => John[4] => Bill)
Array([1] => Andrew[2] => John[3] => Bill)
Array([0] => Andrew[1] => John[3] => Bill)
Array([0] => Andrew)
Analyze the following code snippet that uses the preg_grep() PHP Regular Expression method:
<?php
$names = array('Andrew','John','Peter','Nastin','Bill');
$output = preg_grep('/^[a-m]/i', $names);
print_r( $output );
?>
What will be the output of the above code ?
Array([0] => Andrew[1] => John[4] => Bill)
Array([1] => Andrew[2] => John[3] => Bill)
Array([0] => Andrew[1] => John[3] => Bill)
Array([0] => Andrew)
Analyze the following code:
<?php
$string = 'The Lord is the God';
if(preg_match("/the+(?!is)/i", $string,$match))
{
print_r($match);
}
else
{
echo 'No match found';
}
?>
What will be the output of the above code snippet?
the
Array([0] => The)
The,the
The
Analyze the following code that uses a PHP Regular Expression Character Set:
<?php
$string = "Is this all there is?";
echo(preg_match("/[^A-J]/",$string,$matches));
?>
What will be the output of the above code snippet?
1
|
Is
s
true
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?
Backtrack limit was exhausted!
0
1
null
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?
worldd
world
worlddd
None of the above
Analyze the following code snippet used to check the password strength:
<?php
$password = "Fyfjk34sdfjfsjq7";
if (preg_match("/^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/", $password))
{
echo "Your passwords is strong.";
}
else
{
echo "Your password is weak.";
}
?>
What will be the output of the above code snippet?
Your password is strong
Your password is weak
It will display an error as the output
null
Analyze the following code snippet that uses the preg_replace() PHP Regular Expression method:
<?php
echo preg_replace("/([Cc]opyright) 200(3|4|5|6)/", "$1 2007", "Copyright 2005");
?>
What will be the output of the above code snippet?
copyright 2005
Copyright 2007
Copyright 2005
copyright 2007
Analyze the following code snippet that uses the preg_quote() PHP Regular Expression method:
<?php
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo $keywords;
?>
What will be the output of the above code?
$40 for a g3/400
No output
\$40 for a g3\/400
$40
Analyze the following code:
<?php
$string = 'The GOD is the Lord.';
$string = preg_replace("/The/g", 'Ze', $string);
echo $string;
?>
What will be the output of the above code snippet?
Ze GOD is the Lord.
The GOD is the Lord.
Ze GOD is Ze Lord.
A compilation error will be displayed.
Analyze the following code that uses a PHP Regular Expression Character Set:
<?php
$string = "Is this all there is?";
preg_match_all("/[a-h]/",$string,$matches);
foreach($matches[0] as $value)
{
echo $value;
}
?>
What will be the output of the above code snippet?
h
hahee
h,a,h,e,e
hae
Analyze the following code:
<?php
$string = 'six at noon taxes';
echo preg_match("/s.x/", $string, $matches);
?>
What will be the output of the above code snippet?
six
1
six at noon taxes
null
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?
10
101010
1000
10101000
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?
0
This is the The FOO Way brought to you by BAR
1
This is the The PHP Way brought to you by PHPro.orG
Analyze the following code:
<?php
$string = '%Give_result%';
preg_match_all("/[\W]/", $string, $matches);
foreach($matches[0] as $value)
{
echo $value;
}
?>
What will be the output of the above code snippet?
G,i,v,e,r,e,s,u,l,t
%_%
%%
G
Analyze the following code snippet that uses the preg_split() PHP Regular Expression method:
<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>
What will be the output of the above code?
Array([1] => s[2] => t[3] => r[4] => i[5] => n[6] => g)
Array([0] => s)
Array([0] => s[1] => t[2] => r[3] => i[4] => n[5] => g)
None of the above
Analyze the following code snippet that uses the preg_match() PHP Regular Expression method:
<?php
$string = 'ball';
echo preg_match("/b[aoiu]l/", $string, $matches);
?>
What will be the output of the above code?
1
An array of all matching strings found
The last match found
Only the first matching element
Analyze the following code:
<?php
$str="This lathe turns wood.";
echo(preg_match("/\Bthe\b/", $str));
?>
What will be the output of the above code snippet?
0
the
null
1
Analyze the following code that uses a PHP Regular Expression Character Set:
<?php
$string = "Welcome to the new era";
preg_match_all("/[h-b]/",$string,$matches);
foreach($matches[0] as $value)
{
echo $value;
}
?>
What will be the output of the above code snippet?
null
1
Compilation error will be displayed.
h
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?
[\[\
[\]
[]]
[]
Regular Expressions with PHP Test Fiverr 2020