No regex Lösung:
Ihre Fakten Holen gerade - az ist falsch, einige Buchstaben sind Ähnlichkeit weggelassen wegen:
A Neufundland B Nova Scotia C Prince Edward Island
E New Brunswick G Québec-Ost H Montréal und Laval
J Québec-West K Ontario-Ost L Ontario-Mitte
M Groß-Toronto N Ontario-Südwest P Ontario-Nord
R Manitoba S Saskatchewan T Alberta
V British Columbia X NW-Territ. Nunavut Y Yukon
Code:
def CheckCanadianPostalcodes(p, strictCapitalization=False, fixSpace=True):
'''returns a Tuple of (boolean, string):
- (True, postalCode) or
- (False, error message)
By default lower and upper case characters are allowed,
a missing middle space will be substituted.'''
pc = p.strip() # copy p, strip whitespaces front/end
if fixSpace and len(pc) == 6:
pc = pc[0:3] + " " + pc[3:] # if allowed and needd insert missing space
nums = "" # allowed numbers
alph = "ABCEGHJKLMNPRSTVWXYZ" # allowed characters (WZ handled below)
mustBeNums = [1,4,6] # index of number
mustBeAlph = [0,2,5] # index of character (WZ handled below)
illegalCharacters = [x for x in pc if x not in (nums + alph.lower() + alph + " ")]
if strictCapitalization:
illegalCharacters = [x for x in pc if x not in (alph + nums + " ")]
if illegalCharacters:
return(False, "Illegal characters detected: " + str(illegalCharacters))
postalCode = [x.upper() for x in pc] # copy to uppercase list
if len(postalCode) != 7: # length-validation
return (False, "Length not 7")
for idx in range(0,len(postalCode)): # loop ofer all indexes
ch = postalCode[idx]
if ch in nums and idx not in mustBeNums: # is s number, check index
return (False, "Format not 'ADA DAD'")
elif ch in alph and idx not in mustBeAlph: # id s character, check index
return (False, "Format not 'ADA DAD'") # alpha/digit
elif ch == " " and idx != 3: # is space in between
return (False, "Format not 'ADA DAD'")
if postalCode[0] in "WZ": # no W or Z first char
return (False, "Cant start with W or Z")
return (True,"".join(postalCode)) # yep - all good
testCases = [(True,"A9A 9A9"), (True,"a9a 9a9"), (True,"A9A9A9"), (True,"a9a9a9"),
(False,"w9A 9A9"), (False,"z9a 9a9"), (False,"a9a 9!9")]
for t in testCases:
pc = CheckCanadianPostalcodes(t[1]) # output differs, see func description
assert pc[0] == t[0], "Error in assertion: " + str(t) + " became " + str(pc)
print(t[1], " => ", pc)
pp = input("Postal code: ")
print(CheckCanadianPostalcodes(pp)) # output differs, see func description
Ausgang:
A9A 9A9 => (True, 'A9A 9A9')
a9a 9a9 => (True, 'A9A 9A9')
A9A9A9 => (True, 'A9A 9A9')
a9a9a9 => (True, 'A9A 9A9')
w9A 9A9 => (False, 'Cant start with W or Z')
z9a 9a9 => (False, 'Cant start with W or Z')
a9a 9!9 => (False, "Illegal characters detected: ['!']")
Postal code: b2c3d4
(False, "Illegal characters detected: ['d']")
Press any key to continue . . .
Diese answer with regex (nicht akzeptiert) liefert die richtige regex.
Anzahl möglicher Postleitzahlen (from wikipedia)
Postleitzahlen umfassen nicht die Buchstaben D, F, I, O, Q oder U, und die erste Position auch nicht die Verwendung der machen Buchstaben W oder Z. [...]
As the
Canada Post reserviert einige FSAs für spezielle Funktionen, zB für Test- oder Werbezwecke (zB die H0H 0H0 für Santa Claus, siehe unten) sowie für die Sortierung von Postsendungen für Ziele außerhalb Kanada. [...]
, was Sie mit ABCEGHJKLMNPRSTVXY ohne WZ als 1. char.
Edit: Incoperated Änderung Vorschlag von jl-peyret
- erlaubt fehlenden Raum
- und klarer machen, wenn Groß-/Klein ok
ist
Sie meinen mit regulären Ausdrücken, denke ich? 'import re len (re.findall (pattern, string_to_search))' machen Sie das Muster für die Postleitzahl dann sehen, ob 'Len' größer als 1 sollte funktionieren. –
https://stackoverflow.com/questions/15774555/efficient-regex-for-canadian-postalcode-function –
KEIN Duplikat von [kanadisch-postal-code-validation-python-rege] (https: // stackoverflow.com/questions/29906947/canadian-postal-code-validation-python-regex) noch [JS: efficient-regex-für-kanadische-postal-code-funktion] (https://stackoverflow.com/questions/15774555/efficient-regex-für-kanadische-postal-code-funktion). Die zweite ist Javascript und verwendet verbotene Zeichen, die erste ist keine akzeptierte Antwort (aber eine gültige Regex-Lösung) –