Question

In: Computer Science

Q1. 1. The phone numbers collected from questionnaire is a mess. Design a regular expression to...

Q1. 1. The phone numbers collected from questionnaire is a mess. Design a regular expression to filter out those numbers that are stored in the standard format "+00-0-0000-0000" from the file called "Q1.txt".

Q1.txt

+61392144979

+61 39214 4778

+61-3-9214-4980

+66(2)51574430

+61-3-9285-7706

Note: Only +61-3-9214-4980 and +61-3-9285-7706 are the valid results.

Solutions

Expert Solution

A regular expression is a text string which contains a combination of some special characters called metacharacters and literals and is used to match,search and replace text that follows a certain pattern.They are denoted as “regex” or “regexp” in short hand notation.

Here ,the regular expression to filter out the phone numbers that are stored in the standard format "+00-0-0000-0000" is given below.

\+\d{2}\-\d\-\d{4}\-\d{4}

Now analysing each part of the above regular expression ,

\+ - this portion is used to match the "+" symbol.

Since, "+" symbol is a metacharacter in regular expressions we use a backslash (\) infront of it to consider it as a character.

\d - represents any digit from 0 to 9, ie , this is something equivalent to [0-9] .

{2} - This is a quantifier indicating exactly 2 matches of the preceeding token.So this allows to numbers from 0-9 after the '+' symbol.

\-   - this matches a '-' character.

\d - matches a single number from 0-9.

\-   - this is used again and this matches a '-' character.

\d{4} - matches exactly 4 occurences of digits from 0-9.

\- - this is used again and this matches a '-' character.

\d{4} - matches exactly 4 occurences of digits from 0-9.

So,the regex    \+\d{2}\-\d\-\d{4}\-\d{4}   matches with only two of the above phone numbers and that are +61-3-9214-4980 and +61-3-9285-7706.

The image showing test results with an online tool to test regex is also attached below,

The following is the explanation about some other metacharacters that are used frquently in regular expressions,

/ - a backslash when used with another special cahracter indicates that ,the special cahracter needs to be treated as a literal .

[ ...] - When a certain set of characters are specified within square brackets any of the the characters can match the search string.For example, [0-9] indictes any number between 0 to 9 .

( ) - parenthesis is used to indicate the order of pattern evaluvation and replacement.

^ - usually indicates the beginning of a sentence.

[^...] - here the caret symbol is used to exclude or negate the following characters.

| - The alternation character or bar used to indicate “or” condition .Either of the strings separated by | will be used for matching.

* - The asterik symbol marks zero or more occuernces of characters to the left of the symbol.

? - character indictes zero or more occuernces of characters to the left of the symbol.

. - The dot character is used to match any single character

{} - this is used to limit repetitions by specifying minimaum and maximum number of repetitions as {min,max}

.


Related Solutions

The phone numbers collected from questionnaire is a mess. Design a regular expression to filter out...
The phone numbers collected from questionnaire is a mess. Design a regular expression to filter out those numbers that are stored in the standard format         “+00-0-0000-0000” from the file called “Q1.txt” and redirect the results to the “cleaned.txt”. Note: Only +61-3-9214-4980 and +61-3-9285-7706 are the valid results.    [10 Marks]
Q1 Write a python regular expression to match a certain pattern of phone number.             ###...
Q1 Write a python regular expression to match a certain pattern of phone number.             ### Suppose we want to recognize phone numbers with or without hyphens. The ### regular expression you give should work for any number of groups of any (non- ### empty) size, separated by 1 hyphen. Each group is [0-9]+. ### Hint: Accept "5" but not "-6" ### FSM for TELEPHONE NUMBER IS: # state:1 --[0-9]--> state:2 # state:2 --[0-9]--> state:4 # state:2 --[\-]---> state:3 #...
Design a regular expression to filter out those numbers that are stored in the standard format  “+00-0-0000-0000”....
Design a regular expression to filter out those numbers that are stored in the standard format  “+00-0-0000-0000”. Examples of valid results are: +61-3-9214-4980 and +61-3-9285-7706
1. What is a regular expression? Write a regular expression that will detect “College” and “collegE”....
1. What is a regular expression? Write a regular expression that will detect “College” and “collegE”. 2. What is degree centrality? Create a graph of 4 vertices and compute the degree centrality of the vertices. 3. Compute internal and external community densities for a graph containing 6 nodes. You can create any graph of 6 nodes with at least 4 edges.
Write the correct regular expression pattern for a phone number field with the format XXX-XXX-XXXX
Write the correct regular expression pattern for a phone number field with the format XXX-XXX-XXXX
submit a regular expression that will identify each of the following patterns. 1 - a US...
submit a regular expression that will identify each of the following patterns. 1 - a US telephone number that conforms to the following pattern 111.222.3456 2 - a US social security number that fits the following pattern 111-22-3456 3 - an American Express credit card number that fits the following format:       4 digits followed by a space followed by 6 digits followed by a space followed by 5 digits False positives are allowed in each case, so all you need...
1- Explain following statement:- “The design of your questionnaire will affect the response rate and the...
1- Explain following statement:- “The design of your questionnaire will affect the response rate and the reliability and validity of the data you collect.”
1. Define a regular expression for validating each of the following: a. A U.S. zip code,...
1. Define a regular expression for validating each of the following: a. A U.S. zip code, which must have five digits, followed by an optional hyphen and four digits. b. A telephone number in the format (aaa) nnn-nnnn, where aaa represents the area code and the ns represent digits. c. A date in the form of day-month-year, where the month is a three-letter code and the year is four digits. A hyphen must separate the day and month and year...
QUESTION 1: Assume and design a Questionnaire carrying a minimum of ten important questions with suitable...
QUESTION 1: Assume and design a Questionnaire carrying a minimum of ten important questions with suitable options of your own to know the difficulties faced by the employees of hypermarket as per recent lockdown situation due to Covid-19 situation in Oman. (Instruction: The questionnaire should be made using Simple alternate questions, Specific information questions and Multiple-choice questions.)
1. Provide a regular expression that describes all bit-strings that length is at least one and...
1. Provide a regular expression that describes all bit-strings that length is at least one and at most three. 2. Provide a regular expression that describes all bit strings with odd length.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT