In: Computer Science
Question d) Write the regular expression statement that will match a string that starts with the word "Warning" following by one space and then a number of any number of digits.
Question e) Write the regular expression statement that will match a string that ends with the phrase "End of Report.
Question f) Write the regular expression statement that will match a string that starts with the phrase "Begin Log:" then contains a collection of characters (other than newline) followed by the phrase "End Log".
Question g) Write the regular expression statement that will match the string "Mr.", "Ms." or "Mrs." followed by one or more spaces, followed by one uppercase character then one or more lowercase characters.
Question h) Write the regular expression statement that will match a 10 digit phone number of the format (555-555-5555 or 5555555555 or 555-5555555). We want the pattern to allow an optional dash after the first 3 numbers and after the next three numbers.
Hint use the ? operator.
As per the guidelines ansering only the 4 questions.
d) The below regular expression will fetch as per the requirements. ^ stands for the start of the string. $ stands for the end of the string. [0-9]+ stands for a number between 0-9 any number of times. Please not that the termination character $ is not given at the end of the regular expression, which means this will do a partial match also. for example it will match "Warning 34" and "Warning 34isthis". Please put a $ sign at the end of the below expression if partial match is not required.
^Warning [0-9]+
e) The below regular expression will fetch the details. The ".+" at the beginning takes care of atleast 1 character in the start. The $ at the end takes care of the string ending with "End of Report".
.+End of Report$
f) The below regular expression will help fetching the details. The ".+?" in the middle takkes care of collection of characters in the middle of the string. The ? is a lazy macthing and that will help if more than 1 line exists in the input in the same pattern.
^Begin Log:.+?End Log$
g) The below will take care of the requirement. The pattern (Mr|Ms|Mrs) handles any of the given. The \\. stands for matching a dot character after the given .
(Mr|Ms|Mrs)\\. +[A-Z][a-z]+