I need to validate a Vat number.
xxxx.xxx.xxx --> 0123.456.789 is a valid number.
I found a regex
^(BE)[0-1]{1}[0-9]{9}$|^((BE)|(BE ))[0-开发者_StackOverflow中文版1]{1}(\d{3})([.]{1})(\d{3})([.]{1})(\d{3})
This validate the following entry: BE 0123.456.789.
But what i need is to validate only xxxx.xxx.xxx ( nothing else is valid , only this )
So 4 digits , a point , 3 digits , a point , 3 digits.
Also it needs to begin with 0 or 1 ( first x --> 0 or 1 )
Here you go:
^[01]\d{3}\.\d{3}\.\d{3}$
Breakdown:
^ - Start of string
[01] - Followed by a 0 or 1
\d{3} - Followed by three numerals
\. - Followed by a .
\d{3} - Followed by three numerals
\. - Followed by a .
\d{3} - Followed by three numerals
$ - Followed by end of string
This should work:
^[01]\d{3}\.\d{3}\.\d{3}$
This is the expression
^[0-1]\d{3}[.]\d{3}[.]\d{3}$
^ // start of the input
\d{#} // numbers repeated # times
[.] // literal . (same as \. )
$ // end of the input
If the patter you need to match really is: 4 digits , a point , 3 digits , a point , 3 digits. and begin with 0 or 1
then try this:
^[01]\d{3}\.\d{3}\.\d{3}$
You want:
^[01]\d{3}\.\d{3}\.\d{3}$
As others have pointed out, one way is
^[01]\d{3}\.\d{3}\.\d{3}$
This is the correct regex if you want to allow any digits (incl. non-Arabic ones), as \d
is the same as [:digit:]
, which matches any character marked as a digit in Unicode.
If you only want to allow Arabic digits (and it sounds like you do), you should use [0-9]
instead of \d
:
^[01][0-9]{3}\.[0-9]{3}\.[0-9]{3}$
If that is exactly what you want to validate, you only need this:
^[01]\d{3}\.\d{3}\.\d{3}$
This does exactly what you said in the question: 0 or 1, 3 digits, point, 3 digits, point, 3 digits. Be aware that your regex in your question is more different from what you want than just the BE
, however, if you found it in reference to VAT numbers then it also might be more correct or accept more common forms of VAT numbers.
精彩评论