telephone number regex - regex

I am currently trying to validate UK telephone numbers:
The format I'm looking for is: 01234 567891 or 01234567891 - So I need the number to have 5 numbers then a space then 6 numbers or simply a 11 numbers.
The number must start with a 0.
I've had a look at a couple of examples:
/^[0-9]{10,11} - to check that the chars are all numbers
/^0[0-9]{9,10}$/ - to check that the first number is a 0
I'm just unsure how to put all these together and check if there is a space or not.
Could someone help me with this regex?
Thanks

Try this regex:
/^0\d{4}\s?\d{6}$/

Many people try to do input validation and formatting in a single step.
It is better to separate these processes.
Match UK telephone number in any format
^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$
The above pattern allows the user to enter the number in any format they are comfortable with. Don't constrain the user into entering specific formats.
Extract NSN, prefix and extension
^(\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?)?\(?0?(?:\)[\s-]?)?([1-9]\d{1,4}\)?[\d[\s-]]+)((?:x|ext\.?|\#)\d{3,4})?$
Next, extract the various elements.
$2 will be '44' if international format was used, otherwise assume national format with leading '0'.
$4 contains the extension number if present.
$3 contains the NSN part.
Validation and formatting
Use further RegEx patterns to check the NSN has the right number of digits for this number range. Finally, store the number in E.164 format or display it in E.123 format.
There's a very detailed list of validation and display formatting RegEx patterns for UK numbers at:
http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers
It's too long to reproduce here and it would be difficult to maintain multiple copies of this document.

If you are looking for all UK numbers, I'd look for a bit more than just that number, some are in the format 020 7123 4567 etc.
^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$

/\d*(*)*+*-*/
Simple Telephone Regex includes + () and - anywhere, as well as digits

I think ^0[\d]{4}\s?[\d]{5,6}} will work for you. I have used [\d] instead of [0-9].
I find that RegExr is a useful online tool to check and try your regular expressions. It also has a nice library of examples to help point you in the right direction

you should just count the number of digits and check that it's 10,
Some UK numbers have only 9 digits, not 10 (not including the leading 0).
These include 40 of the 01 area codes (using "4+5" format), the 016977 area code (using "5+4" format), all 0500 numbers and some 0800 numbers.
There's a list at: http://www.aa-asterisk.org.uk/index.php/01_numbers

This US numbers pattern accepts following phones as well:
800-432-4500, Opt: 9, Ext: 100316
800-432-4500, Opt: 9, Ext: X100316
800-432-4500, Option #3
(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4}),?(?:\s*(?:#|x\.?|opt(\.|:|\.:)?|option)\s*#?(\d+))?,?(?:\s*(?:#|x\.?|ext(\.|:|\.:)?|extension)\s*(\d+))?
(used this answer in other topic as start point)

Related

Regex to match a few specific 6 digit numbers (Indian pin codes)

I am using Google Forms to create an application form. Need to restrict submission for a few specific pin codes only. Here are some of the pin codes that I'm trying to limit it to.
560078 560070 560085 560069 560011 560080 560004 570070 560089 560060
First 4 digits are same in all the pin codes. I need to match the last two digits to the ones from a list. The list might end up being about 30, hence looking for a regex.
Which regex should I use for that?
You haven't specified the exact ranges. The pattern 5600(04|11|[6-8]\d) will match:
560004
560011
all the numbers from 560060 to 560089
It will need to be corrected according to the acceptable ranges.
It is suprising to see 570070 in the list when you note that the first 4 digits are always the same. Is it an error or an excpetion.

Regex to extract UK Currency including £ symbol and Pence (p)

I am fairly new to RegEx and have had a search around online but am unable to find a regex that fits my requirements.
The ultimate aim is to search a string of text and extract the lowest monetary amount, however as the string may contain more than one £amount, then i'm happy for a regex to just extract all monetary values it can find and then I can write a calculation in order to return the lowest amount.
The string may have numbers that are not monetary values / numerous amounts, therefore the regex should always look for a £ symbol first OR it could end with a "p" or "P" to signify pence. For example "I need 2 of these at £10 each and one of those at 50p" - should return 10.00 & 0.50 - I can then calculate that 0.50 is the lowest amount.
As people also write their amounts in various ways, I need the regex to be able to spot different patterns - including the "," for every thousand. All below values should be valid:
£0
£0.00
£0.00p
£0000
£0000.00
£0000.00p
£0,000
£0,000.00
£0,000.00p
0p
Hopefully someone may be able to advise the best way to approach this.
Thanks
This works on your data set:
(?=^£|.*p$)£?\d*(?:,\d{3})*(\.\d{2})?p?
But it may improperly match some edge cases as well because everything is optional...
https://regex101.com/r/WptUn6/3

Regular expression for all bank card numbers

I am using a regular expression to match all UK bank card number formats; I have done research and managed to find/amend a regex that covers the majority of formats. However, I have a bit of an edge case where one is not matching and I do not know why, or how to resolve. This is what I am using:
(\b[4|5|6](\d){3}[\s|-]?((\d){4}[\s|-]?){2}(\d){4}\b)|(\b(\d){4}[\s|-]?(\d){6}[\s|-]?(\d){5}\b)
This is an example card number that does not work: 6759000000005
This is an example card number that does work: 675900000000555
Apologies if this is an easy question, I am fairly new to regular expression syntax. Any help to resolve would be greatly appreciated. Thanks.
See here the demo
The regex is (\b[4|5|6]\d{3}[\s-]?(\d{4}[\s-]?){2}\d{1,4}\b)|(\b\d{4}[\s-]?\d{6}[\s-]?\d{5}\b)
I'm not an expert of UK cards, so I can't tell what is the expected format as you did not gave exemples with spaces or hyphens in them...
If you can refine the requirements it's handlable.
A more generic card number validation (without separators, so you'll need to strip them before) would be
\d{6}\d{1,12}\d
As per the requirements of the norm (found nothing on the minimum length of the account identifier):
An ISO/IEC 7812 card number is most commonly 16 digits in length,[1]
and can be up to 19 digits. The structure is as follows:
a six-digit Issuer Identification Number (IIN) (previously called the
"Bank Identification Number" (BIN)) the first digit of which is the
Major Industry Identifier (MII), a variable length (up to 12 digits)
individual account identifier, a single check digit calculated using
the Luhn algorithm.[2]

Regex to Limit Input To Only Numeric Values To 10 Occurences

I need help forming regex to limit user input to only numerics and only up to 10 occurrences.
I have regex that is working to keep input to numerics only, but I cannot limit it to up to 10.
Here is what I have:
^(0|[1-9][0-9]*)$
I am okay accepting negative numbers, decimals, and 0's. Any advice?
^\s*([0-9)+){0,10}\s*$
This basically says I want to 0 to 10 things, where each thing is all digits. I added the \s* on either side to allow the user to have put spaces before or after their numbers. This would accept things like
10 1231231 1231 1231 23112 123123
If what you really want is just a single number, that is only up to 10 digits, it is even easier:
^\s[0-9]{1,10}\s$
The regex you're looking for is this:
/(?=^[-+]?\d*\.?\d+$)^.{1,10}$/
Keep in mind that this regex will allow maximum length of input to 10 which includes optional + or - sign at start and a decimal point ..
you can try this:
^(?<=\s)(\-?[\d]{1,10}(?=\s))$
This fails as it has 11 digits
Debug.WriteLine(Regex.IsMatch("12345678901", #"^\d{1,10}$").ToString());
Posted the above answer before you clarified you want up to 10 set of numbers delimited by space.
Tested the accepted answer and in .NET Regex it fails for me.
Even fixing the syntax error it still does not parse by space
Give this a try
Debug.WriteLine(Regex.IsMatch(" 12345 678901 12 ", #"^\s*([+-]?\d+)(\s+[+-]?\d+){1,9}\s*$").ToString());

find a string with at least n matching elements

I have a list of numbers that I want to find at least 3 of...
here is an example
I have a large list of numbers in a sql database in the format of (for example)
01-02-03-04-05-06
06-08-19-24-25-36
etc etc
basically 6 random numbers between 0 and 99.
Now I want to find the strings where at least 3 of a set of given numbers occurs.
For example:
given: 01-02-03-10-11-12
return the strings that have at least 3 of those numbers in them.
eg
01-05-06-09-10-12 would match
03-08-10-12-18-22 would match
03-09-12-18-22-38 would not
I am thinking that there might be some algorithm or even regular expression that could match this... but my lack of computer science textbook experience is tripping me up I think.
No - this is not a homework question! This is for an actual application!
I am developing in ruby, but any language answer would be appreciated
You can use a string replacement to replace - with | to turn 01-02-03-10-11-12 into 01|02|03|10|11|12. Then wrap it like this:
((01|02|03|10|11|12).*){3}
This will find any of the digit pairs, then ignore any number of characters... 3 times. If it matches, then success.