0

I've had an error come up with my regex in Ruby - The postcode BT9 6NZ managed to slip through written as BT96NZ - is there any way to edit this to make it foolproof to that?

The code as I have it is below:

^BT\d{1,2}\s|^HS\d{1,2}\s|KA27|AB31|AB32|AB33|AB34|AB35|AB36|AB37|AB38|PH4|PH5|PH6|PH7|PH8|PH9|KA28|^KW\d{1,2}\s|^IM\d{1,2}\s|^IV\d{1,2}\s|^AB4\d{1}\s|^AB5\d{1}\s|^PA2\d{1}\s|^PA3\d{1}\s|^PA4\d{1}\s|^PA5\d{1}\s|^PA6\d{1}\s|^PA7\d{1}\s|^PH1\d{1}\s|^PH2\d{1}\s|^PH3\d{1}\s|^PH4\d{1}\s|^PH5\d{1}\s
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116

1 Answers1

0

The Regex you've shown in your question doesn't match 'BT96NZ'

/^BT\d{1,2}\s|^HS\d{1,2}\s|KA27|AB31|AB32|AB33|AB34|AB35|AB36|AB37|AB38|PH4|PH5|PH6|PH7|PH8|PH9|KA28|^KW\d{1,2}\s|^IM\d{1,2}\s|^IV\d{1,2}\s|^AB4\d{1}\s|^AB5\d{1}\s|^PA2\d{1}\s|^PA3\d{1}\s|^PA4\d{1}\s|^PA5\d{1}\s|^PA6\d{1}\s|^PA7\d{1}\s|^PH1\d{1}\s|^PH2\d{1}\s|^PH3\d{1}\s|^PH4\d{1}\s|^PH5\d{1}\s/.match? 'BT96NZ'
=> false

/^BT\d{1,2}\s|^HS\d{1,2}\s|KA27|AB31|AB32|AB33|AB34|AB35|AB36|AB37|AB38|PH4|PH5|PH6|PH7|PH8|PH9|KA28|^KW\d{1,2}\s|^IM\d{1,2}\s|^IV\d{1,2}\s|^AB4\d{1}\s|^AB5\d{1}\s|^PA2\d{1}\s|^PA3\d{1}\s|^PA4\d{1}\s|^PA5\d{1}\s|^PA6\d{1}\s|^PA7\d{1}\s|^PH1\d{1}\s|^PH2\d{1}\s|^PH3\d{1}\s|^PH4\d{1}\s|^PH5\d{1}\s/.match? 'BT9 6NZ'
=> true

so I don't think your issue is the regex but more likely the code where you're using it to filter the parameter.

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78