In: Computer Science
For each of the strings below, does the pattern [^O]*o+ match the string? If so, list which part of the string will be matched. If not, then state "no match". (a) balloons (b) FOODY (c) bookstore (d) "Look" (e) PoolRoOM
Firstly let us break down the pattern [^O]*o+ for more understanding of which part of the strings which be matched.
[^O] - This part is telling us to stop matching the strings once they contain O in it.
[^O]* - It is just a character class repeating itself for zero or more consecutive occurrences.
o+ - This part will only match the strings that have at least o in it.
So, the pattern [^O]*o+ will only match those strings that are having at least one o in it and will match up to the last occurrence of o in the string to be matched and not starting with O. Where ever O will occur, the pattern would stop matching. So the word FOODY will not be matched by the pattern.
balloons - the string will get matched up to balloo. Here's a screenshot of the code verifying the same.
FOODY - This word will get no match. Hence 'No match' is the answer. Since the word does not contain even a single o.
The array which we get as a result of word.match is null that is we did not get any matches.
bookstore - Since the text will get matched up to the last occurrence of o. So the answer here would be booksto.
"Look" - Since the text will get matched up to the last occurrence of o. So the answer here would be "Loo.
PoolRoOM - Since the text will get matched up to the last occurrence of o and the string will stop matching once we get even a single occurrence of O. So the answer here would be PoolRo.
Let me know if you have any issues.
Thank you.
Have a good day.