Ignore spaces and punctuation for VAT number using Regex

时间:2015-07-28 22:44:00

标签: regex expression

I'm a newbie at regex and I'm trying to ignore spaces and punctuation when validating the format of a VAT number.

I have this regular expression: ^BE[0-9]{10,10}$

It works for this: BE1234567890

But I need it to work for these too:

  • BE 1234567890
  • BE 123.456.7890
  • BE:1234567890
  • etc.

How can I get the Regex to ignore whitespaces and punctuation that I define?

2 个答案:

答案 0 :(得分:0)

BE(\s?|:)(\d{10}|\d{3}.\d{3}.\d{3,4})

this is for EXCACTLY your given examples. Note that you might match more digits in the last \d{3,4} to fulfill your needs

答案 1 :(得分:0)

A better idea would be just to strip all spaces and punctuation beforehand, then validate the stripped string with your regex.

But if you really want to, you can do this:

^BE([[:space:][:punct:]]*[0-9]){10}$

This allows arbitrary spaces/punctuation before each digit.