I am new to python and I hope someone can help me with this. I need to extract credit card numbers from a string. e.g
"My credit card number is 1234-2312-2312-2312" or "My Credict card number is 1234 1234 1832 1234"
Anyone knows how I can do it?
Do it like this using regex
import re
def findCardNumber(string):
pattern = r"(^|\s+)(\d{4}[ -]\d{4}[ -]\d{4}[ -]\d{4})(?:\s+|$)"
match = re.search(pattern, string)
if match:
print(match.group(0))
findCardNumber("My Credict card number is 1234 1234 1832 1234")
This also considers the location of your card number in the string, it can be anywhere - at the beginning, somewhere in the middle or at the end.