In: Computer Science
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 to do is find patterns that fit your Regular Expression.
Make sure you test the Regular Expression in Linux and submit a screenshot of the expression and result.
Hi,
For First Solution Regular Expression-
^[0-9][0-9][0-9][.][0-9][0-9][0-9][.][0-9][0-9][0-9][0-9]$
^ means it will start with the character here the succeding character is a digit. So it should start always with a digit.
[0-9] means any single digit. If we write again [0-9] then that means another single digit.
[.] means single character dot(.)
$ means the end of the expression. Here from the regular expression the last character shuld be a digit
For Second Solution Regular Expression-
^[0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9][0-9][0-9]$
^ means it will start with the character here the succeding character is a digit. So it should start always with a digit.
[0-9] means any single digit. If we write again [0-9] then that means another single digit.
[-] means single character hyphen(-)
$ means the end of the expression. Here from the regular expression the last character shuld be a digit
For Third Solution Regular Expression-
^[0-9][0-9][0-9][0-9][[:space:]][0-9][0-9][0-9][0-9][0-9][0-9][[:space:]][0-9][0-9][0-9][0-9][0-9]$
^ means it will start with the character here the succeding character is a digit. So it should start always with a digit.
[0-9] means any single digit. If we write again [0-9] then that means another single digit.
[[:space:]] means single character space( )
$ means the end of the expression. Here from the regular expression the last character shuld be a digit.
From below screenshot I have created a text file where there are seven combination of numbers including three combinations also I have mentioned which was mentioned in your question-
So from the below screenshot, I will use the above three regular expressions that will let us to find those three combination of expressions
As you can see from the above screenshot with the help of those three regular expressions I am able to find those three expressions.
First I am using cat for that file i.e, in my case text.txt and then doing a pipe(|) which will allow us to grep for that particular regular expression