Question

In: Computer Science

Python Programming: Write a RegEx that validates social security numbers

Python Programming:

Write a RegEx that validates social security numbers

Solutions

Expert Solution

There a very simple module in python that can match given input with a regex pattern.

For a Social Security Number to be valid, it must satisfy the following constraints:

  1. It should be divided into 3 parts by a hyphen (-).
  2. It should 9 digits in length.
  3. The first part must consist of 3 digits and should not be 000, 666, or between 900 and 999.
  4. The second part must consist of 2 digits and it should be from 01 to 99.
  5. The third part must consist of 4 digits and it should be from 0001 to 9999.
import re

data = input("Enter your social security number")

print(data)

pattern = "^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$"

if (re.match(pattern,data)):
    print("The SSN is valid")
else:
    print("The SSN is invalid")


Related Solutions

Python regex How to write the regex in Python that only include the contents within a...
Python regex How to write the regex in Python that only include the contents within a parentheses? e.g. "uio" [ek3k 0die] 4229834 return "ek3k 0die"
Regarding Python. 1. How would you write a regex that matches the hour of the day...
Regarding Python. 1. How would you write a regex that matches the hour of the day from the extracted line shown below? (The highlighted portion) From [email protected] Sat Jan 5 09:14:16 2008 Question options: ˆFrom .+ [0-9] ˆX-.*: [0-9.]+ [0-9]: ˆFrom .* [0-9][0-9]: ˆFrom .* ('ˆX\S*: ([0-9.]+)', line) A _____________ sign at the end of the regular expression indicates that the string must end with the specified regex pattern. Question options: question (?) caret (^) dollar ($) 0 / 1...
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the...
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the user to enter a series of positive numbers. The user should enter "0" (zero) to signal the end of the series. After all the positive numbers have been entered, the program should display their sum. The program should display the following: • Contain proper indentation. • Comments (Blocks or Lines). • Good Variables initializations. • Good questions asking for the parameters of the code....
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale...
Programming Python Jupiter notebook Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range, it asks him/her to enter a numeric input in the correct range, instead of returning an error. Example: Enter your score: 78 Letter...
* Python * * Python Programming * Part 1: Product Class Write a class named Product...
* Python * * Python Programming * Part 1: Product Class Write a class named Product that holds data about an item in a retail store.   The class should store the following data in attributes: product id, item description, units in inventory, and price. Write the __init__ method to require all four attributes. Also write a __str__ method for debugging output.    Once you have written the class, write a main() function that creates three Product objects and stores the following...
This is an exercise to design and write a Python program in good programming style for...
This is an exercise to design and write a Python program in good programming style for a simulation of stock price over a period of 100 days. In this exercise, you are asked to simulate the stock price starting at $100.00 for 100 days with a daily fluctuation based on the Normal Distribution with mean = 0.0 & sigma = 0.0125. The program will show the daily stock price, the 7-day minimum, the 7-day maximum, the 7-day average, and the...
According to a survey, 47% of adults are concerned that Social Security numbers are used for...
According to a survey, 47% of adults are concerned that Social Security numbers are used for general identification. For a group of eight adults selected at random, we used Minitab to generate the binomial probability distribution and the cumulative binomial probability distribution (menu selections right-pointing arrowhead Calc right-pointing arrowhead Probability Distributions right-pointing arrowhead Binomial). Number r 0 1 2 3 4 5 6 7 8 P(r) 0.006226 0.044169 0.137091 0.243143 0.269521 0.191208 0.084781 0.021481 0.002381 P(<=r) 0.00623 0.05040 0.18749 0.43063...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program...
1. INTRODUCTION The goal of this programming assignment is for students to write a Python program that uses repetition (i.e. “loops”) and decision structures to solve a problem. 2. PROBLEM DEFINITION  Write a Python program that performs simple math operations. It will present the user with a menu and prompt the user for an option to be selected, such as: (1) addition (2) subtraction (3) multiplication (4) division (5) quit Please select an option (1 – 5) from the...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the users enters a negative number. The program then prints out: a) the sum of all the numbers b) the average of all the numbers c) the max of the numbers d) the min of the numbers Note we did not cover lists (array) so you will not use them in this problem. Finally, ask the user for their name, then print their name as...
In python of Jupiter notebook Write a python function called trng that takes three numbers x,...
In python of Jupiter notebook Write a python function called trng that takes three numbers x, y, and z, and specifies if those can form a triangle (i.e., returns the word triangle if they can, and Not a triangleotherwise). Note: In order for three numbers to form a triangle sum of any two of them must be greater than the third one (e.g., x=1, y=2, z=4 cannot form a triangle because x+y is not greater than z even though x+z>y...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT