Shell Create regular expressions that match the following conditions. Use http:/
ID: 3666690 • Letter: S
Question
Shell
Create regular expressions that match the following conditions. Use http://rubular.com/or http://regexpal.com/to develop and test your expressions. Answers may vary. Email addresses. Telephone numbers in either of the following formats: (555)555-5555, 555-555-5555, or 555-5555. Zip codes in either of the following formats: 55555 or 55555-5555. Visa numbers start with 4 and are 13 or 16 digits. MasterCard numbers are always has 16 digits, the first is always 5, and the second digit is 1 through 5 American Express numbers are 15 digits and start with 34 or 37. Diners Club numbers are 14 digits and begin with 300 through 305, 36, or 38. All repeating words which are space delimited. The words should be adjacent. HTML text matching H1-H6 or hl-h6 tags. For example, the following entire string should be matched: text .Explanation / Answer
^s*(?:+?(d{1,3}))?[-. (]*(d{3})[-. )]*(d{3})[-. ]*(d{4})(?: *x(d+))?s*$
It would match the following examples and much more:
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
800 555 1234x5678
8005551234 x5678
1 800 555-1234
1----800----555-1234
Regardless of the way the phone number is entered, the capture groups can be used to breakdown the phone number so you can process it in your code.
Group1: Country Code (ex: 1 or 86)
Group2: Area Code (ex: 800)
Group3: Exchange (ex: 555)
Group4: Subscriber Number (ex: 1234)
Group5: Extension (ex: 5678)
Here is a breakdown of the expression if you're interested:
^s* #Line start, match any whitespaces at the beginning if any.
(?:+?(d{1,3}))? #GROUP 1: The country code. Optional.
[-. (]* #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(d{3}) #GROUP 2: The Area Code. Required.
[-. )]* #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(d{3}) #GROUP 3: The Exchange number. Required.
[-. ]* #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(d{4}) #Group 4: The Subscriber Number. Required.
(?: *x(d+))? #Group 5: The Extension number. Optional.
s*$ #Match any ending whitespaces if any and the end of string.
pin code
^d{5}(?:[-s]d{4})?$
· ^ = Start of the string.
· d{5} = Match 5 digits (for condition 1, 2, 3)
· (?:…) = Grouping
· [-s] = Match a space (for condition 3) or a hyphen (for condition 2)
· d{4} = Match 4 digits (for condition 2, 3)
· …? = The pattern before it is optional (for condition 1)
· $ = End of the string.
matches Visa, MasterCard, American Express, Diners Club, Discover, and JCB cards:
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35d{3})d{11})$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.