Hi Folks,
I am having a little difficulty implementing a PB/Win 9.01 regular expression. It is based on a UK Government published regular expression which is designed to validate UK postcodes. My PB version of the regex looks as though it should work to me but it just isn't matching as I expect.
I have prepared an example program below including notes etc. and would be grateful if anyone would take a look at it to see if I am doing something wrong.
Any comments gratefully received.
Thanks,
David
I am having a little difficulty implementing a PB/Win 9.01 regular expression. It is based on a UK Government published regular expression which is designed to validate UK postcodes. My PB version of the regex looks as though it should work to me but it just isn't matching as I expect.
I have prepared an example program below including notes etc. and would be grateful if anyone would take a look at it to see if I am doing something wrong.
Any comments gratefully received.
Thanks,
David
Code:
' Module Name : postcode_regex_test.bas ' Platform : PB/Win 9.01 ' Windows XP Professional SP3 ' ' Purpose : To implement a PB version of the the UK BS7666 ' postcode validation regular expression provided ' at... ' ' http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd ' ' The 'complex pattern' for UK postcodes ' (to be found under the PostCodeType node of the schema) ' is as follows... ' ' "(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})" ' ' This pattern implements the rules given at... ' http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm ' ' Issue : My PB version doesn't work, am I doing something wrong? #COMPILE EXE #DIM ALL FUNCTION PBMAIN () AS LONG DIM a AS STRING DIM b AS STRING DIM c AS STRING DIM position AS LONG DIM length AS LONG 'Un-comment the following postcode lines one at a time 'and re-compile to test each one. 'Example UK Postcode Format a$ = "GIR 0AA" '1) GIR 0AA - matches 'a$ = "M1 1AA" '2) AN NAA - no match 'a$ = "M60 1NW" '3) ANN NAA - no match 'a$ = "CR2 6XH" '4) AAN NAA - no match 'a$ = "DN55 1PT" '5) AANN NAA - no match 'a$ = "W1A 1HQ" '6) ANA NAA - no match 'a$ = "EC1A 1BB" '7) AANA NAA - no match 'test 1 - A PB compatible version of the BS7666 'postcode validation regular expression. 'It produces no matches on the example postcodes 'numbered 2 to 7, but I think perhaps it should. ' '*NOTE: The individual sub-patterns match data ok 'when tested independently of the full expression. b$ = "(GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|" b$ = b$ & "(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|" b$ = b$ & "(([A-PR-UWYZ][0-9][A-HJKSTUW])|" b$ = b$ & "([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))" b$ = b$ & " [0-9][ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z])" 'When the regex above is tabbed out for clarity as shown 'below, it all seems logical. However only the (GIR 0AA) pattern 'produces a match against the example postcodes listed above. ' '( 'GIR 0AA ') '| '( ' ( ' ([A-PR-UWYZ][0-9][0-9]?) ' | ' ( ' ([A-PR-UWYZ][A-HK-Y][0-9][0-9]?) ' | ' ( ' ([A-PR-UWYZ][0-9][A-HJKSTUW]) ' | ' ([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]) ' ) ' ) ' ) ' [0-9][ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z] ') RESET position& : RESET length& DO position& = position& + length& REGEXPR b$ IN a$ AT position& TO _ position&, length& IF position& <> 0 THEN c$ = "Match at position : " & FORMAT$(position&,"00") _ & " Data : " & CHR$(34) & MID$(a$,position&,length) & CHR$(34) MSGBOX c$ END IF LOOP WHILE position& END FUNCTION
Comment