Help with PHP with regular expression.. please ready the //comment on the code:
ID: 3812518 • Letter: H
Question
Help with PHP with regular expression.. please ready the //comment on the code:
Modify the attached regexp.php scripts to perform the following tasks:
Search for a word in $cities that ends in is. Store this word in the element 0 of the array named $results.
Search for a work in $cities that begins with m and ends in a. Store this word in the element 1 of the array.
Search for a word in $cities that begins with D and ends in h. Store this word in the element 2 of the array.
Search for a word in $cities that ends in o. Store this word in the element 2 of the array.
Output the array $results to the browser.
here is the code for regexp.php
<?
$cities = "minneapolis dytonh Duluth Minnetonka Mankato Edina Egan";
//city that ends with is
$ptrn1 = "/(w*is)/";
preg_match($ptrn1, $cities, $matches);
$results[0] = $matches[0];
//city that begins with m and ends a
//$ptrn2 = "TBD";
preg_match($ptrn2, $cities, $matches);
$results[1] = $matches[0];
//city begins with D and ends with h
//$ptrn3 = "TBD";
preg_match($ptrn3, $cities, $matches);
$results[2] = $matches[0];
//city that ends in o.
//$ptrn4 = "TBD";
preg_match($ptrn4, $cities, $matches);
$results[3] = $matches[1];
//output all the matches
foreach ($results as $match)
{
echo "$match <br>";
}
?>
my out OUTPUT should look like this:
minneapolis
Minnetonka
Duluth
Mankato
Explanation / Answer
The required regex are as follows:
//city that begins with m and ends a
$ptrn2 = "/(mw*a)/";
//city begins with D and ends with h
$ptrn3 = "/(Dw*h)/";
//city that ends in o.
$ptrn4 = "/(w*o)/";
In the regex pattern, means the word boundary. So, only the distinct word will be matched and not the partial word will be matched when you specify .
For example, if you have a sentence and you have define a pattern to find the word "web" with specified, if your sentence contains the word "onweb", the pattern will not be matched as web is not the complete word, it is the partial word of "onweb".
So, in all the regular expressions, the words are found if they are complete words.
So, in these patterns, I have used similar approach as $ptrn1.
Say for example, you are writing for $ptrn2. In which first letter should be 'm'. So, after , inside the (), start with writing m. Now, m can be followed by any word character, which is specified by w. This w can be written 0 or more times and the last character should be 'a'. So, 0 or more means *. As it is possible that the characters will differ in between m and a but they will be words so can identify them with w*.
Similar is with pattern3 and pattern4. In pattern 3, start with D and end with h so as I wrote in $ptrn2, this becomes Dw*h inside the brackets that specify the word boundary using .
In pattern 4, string is ending in 'o'. So, before that, any number of word characters can be there. So this can be written as : w*o (any number of characters, at last 'o').
Note: Regular expressions are always case sensitive. So, if you are trying to get the 4 outputs you mentioned
minneapolis
Minnetonka
Duluth
Mankato
The 2nd output won't come in $ptrn2 because it is defined as starting with 'm' and as this is case sensitive, the 'M' is not the same.
So, in your inputs, if this is the issue and you want to resolve it, I am writing the patterns again with checks for the same characters, but it won't check the character case.(upper case or lower case)
//city that begins with m and ends a
$ptrn2 = "/([Mm]w*[Aa])/";
//city begins with D and ends with h
$ptrn3 = "/([Dd]w*[Hh])/";
//city that ends in o.
$ptrn4 = "/(w*[Oo])/";
Here, I have used [ ]s which means match 1 character from the characters written inside [ ]. So, [Mm] means if the character is 'm' or 'M', it is valid. So, this is the thing you need to add if you want to ignore the case of the character while comparison.
Use the appropriate pattern as per your applications. Seeing your outputs I am suggesting you the second one.
Please comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.