问题
I'm looking for a regular expression to match a Dutch phone number. These are the formatting requirements:
- Should start with 0
- Contains maximum of 1 (optional) dash "-" character, for now it does not matter where it is, as long as it's not the first character
- Total length 10 or 11 characters
This is what I've come up with so far:
^0+-{1}?([0-9]{10,11})$
回答1:
I have seen this before at http://regexlib.com/Search.aspx?k=phone%20number Check this website out, hope it helps.
(^\+[0-9]{2}|^\+[0-9]{2}\(0\)|^\(\+[0-9]{2}\)\(0\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\-\s]{10}$)
Regular expression to evaluate dutch-style phone numbers. Possible example prefixes: +31, +31(0), (+31)(0), 0, 0031 followed by 9 numbers (which can contain a space or -).
回答2:
Try this regex: ^(?=^.{10,11}$)0\d*-?\d*$
回答3:
I came up with this creation
^(?:0|(?:\+|00) ?31 ?)(?:(?:[1-9] ?(?:[0-9] ?){8})|(?:6 ?-? ?[1-9] ?(?:[0-9] ?){7})|(?:[1,2,3,4,5,7,8,9]\d ?-? ?[1-9] ?(?:[0-9] ?){6})|(?:[1,2,3,4,5,7,8,9]\d{2} ?-? ?[1-9] ?(?:[0-9] ?){5}))$
回答4:
Here is a regex for Dutch mobile phonenumbers including a check for international prefixes (0031 or +31). Check it at: https://regex101.com/r/Y2DG19/1
^\(?([+]31|0031|0)-?6(\s?|-)([0-9]\s{0,3}){8}$
Hope this helps people out.
回答5:
here's my solution:
extension String {
func isValidPhone() -> Bool {
let dutchRegex = "^(06[0-9]{8}|[+]{1}31[0]?[0-9]{9,10}|0031[0]?[0-9]{9,10})"
let phonePredicate = NSPredicate(format:"SELF MATCHES %@", dutchRegex)
return phonePredicate(with: self)
}
}
This will validate all numbers with +31, 06, 0031 so will only work for Dutch phone numbers
回答6:
You can also do it like this. First you remove all not digits and then you check the number with the regex.
const regex = /^((\+|00)?31|0(?!0))(\d{9})$/;
const valid = regex.test(input.replace(/\D/g, ''));
It works with numbers starting with 0031
, +31
, 0
.
回答7:
I tried to elaborate on @Raj's solution by adding some more constraints on the use of hyphens or spaces, because it somehow captured 06-01-2019 as well. I placed some comments in there as well to provide examples:
\b(\+[0-9]{2}(?# e.g. +31)|^\+[0-9]{2}\(0\)|\(\+[0-9]{2}\)\(0\)(?# e.g. +31(0)|00[0-9]{2}(?# e.g. 0031)|0)([6][\-\s]?[1-9][0-9]{7}(?# e.g. 0612345678 or 06-12345678 or 06 12345678)|[6][\-\s]?[1-9][0-9]\s?[0-9]{2}\s?[0-9]{2}\s?[0-9]{2}(?# e.g. 06-12 34 56 78)|[6][\-\s]?[1-9][0-9]\s?[0-9]{3}\s?[0-9]{3}(?# e.g. 06-12 345 678)|[6][\-\s]?[1-9][0-9]{2}\s?[0-9]{3}\s?[0-9]{2}(?# e.g. 06-123 456 78)|[0-9]{2}[\-\s]?[1-9][0-9]{6}(?# e.g. 030-1234567)|[0-9]{2}[\-\s]?[1-9][0-9]{2}\s?[0-9]{2}\s?[0-9]{2}(?# e.g. 030-123 45 67)|[0-9]{2}[\-\s]?[1-9][0-9]\s?[0-9]{2}\s?[0-9]{3}(?# e.g. 030-12 34 567))\b
(I know... And I didn't even cover all cases containing whitespaces)
回答8:
I have generated the following one for Dutch mobile numbers. Until now, I tested with some possibilities and was able to cover them. I hope it helps.
\(?(31\s*|0)-?6(\s*\)?|-?\s*)([0-9]\s{0,3}){8}
来源:https://stackoverflow.com/questions/17949757/regular-expression-for-dutch-phone-number