I have the following 2 regex for UK mobile phone number and for UK landline numbers respectively-
^0(7\\d{9})\$
^0([1-6]\\d{8,10})\
I need to combine them into one regex to validate an input field using JavaScript to be any UK mobile or landline. I’ve tried using the | (pipe symbol) for ‘or’, but that’s not working and doesn’t recognise either of them.
Anyone any suggestions as to how best to combine.
Many thanks in advance,
If you want to combine the patterns, you may use
^0([1-6][0-9]{8,10}|7[0-9]{9})$
Note that the common prefix is outside the parentheses, the rest is an alternation group with anchors outside of the group.
Details
^ - start of string
0 - a zero
([1-6][0-9]{8,10}|7[0-9]{9}) - either of the two alternatives
[1-6][0-9]{8,10} - a digit from 1 to 6 and then 8 to 10 digits
| - or
7[0-9]{9} - 7 and any 9 digits
$ - end of string.
Related
We have created a product feed from a Magento webshop using the Koongo Module. We want to submit this feed to a marketplace. Our customer does not consistently fill in the size field when adding products. As a result, there is also an incorrect format for the marketplace. Size examples are: 39/6 or 40 / 6.5 or 8/42 or 8.5 / 42.5. In short, either first the European size and then the UK size or first the UK size and then the European size. We want to correctly display the size in the feed using a regular expression, namely only the European size. In short, we no longer want to include either '/ UK m' or 'UK size /'. It is important that the sign / must also be removed from the outcome in our product feed. Can you help?
We have the option to fill in two fields, namely 'Rewrite from' and 'Rewrite to'. We can use a regular expression for each of these fields.
Thanks in advance for the help.
It would be simple to only take the numbers >= 20. Does that help you?
[2-5][0-9](\.5)?
This will match all sizes between 20 and 59 with the option of half sizes
I am not familiar with Magento. If it supports regular expression replace with capture groups try this:
Rewrite From: ^.*?([234][0-9](\.5)?).*?$
Rewrite To: $1
Explanation:
^ - anchor at start of string
.*? - non-greedy scan
( - capture group start
[234][0-9] - number between 20 and 49
(\.5)? - optional .5 fraction
) - capture group end
.*? - non-greedy scan
$ - anchor at end of string
use capture group in replace: $1
I use a regular expression to validate a Dutch mobile phone number:
^((\+316|06|00316){1}\\s?-?\s?[1-9]{1}[0-9]{7})$
So the number should start with 06 or +316 or 00316 followed by 8 digits (not starting with a 0)
Now I have to extend the validation so it also allows for international numbers from outside The Netherlands.
I want to keep it simple so I was thinking:
If it not starts with 06 or +316 or 00316 then it should start with '00dd' or '+dd' where dd!=31 followed by 8 till 12 digits.
so 0032127384562 should match but 00317198765432 not (because it starts with 0031 but not 00316)
Does anybody know how to do this?
The regex for the international number is:
^00(?!31)[0-9]{2}\s?-?\s?[1-9][0-9]{7,11}$
or, if you can't use lookahead:
^00([0-24-9][0-9]|3[02-9])\s?-?\s?[1-9][0-9]{7,11}$
You can concatenate it with | and the Dutch regex.
Be aware that there is a typo in your regex at \\.
I am new to regex and I an trying use regex to find any number of characters that comes after the question mark and before the pipe, along with the phone number. Can anyone please help me? Below is a sample URL ?
http://www.myurl.com?demo|15555555555
regular expression should find "demo" and "15555555555"
here is a regex with named capturing groups, also I tried to cover many possibilities for different number formats
Regex
(?<=\?)(?'name'[\w\s%]+)\|(?'phone'[\+\w-\(\)\s%]+)(?:\n|$)
Test string
http://www.myurl.com?demo|15555555555
http://www.myurl.com?Local|754-3010
http://www.myurl.com?Domestic|(541) 754-3010
http://www.myurl.com?International|+1-541-754-3010
http://www.myurl.com?Dialed in the US|1-541-754-3010
http://www.myurl.com?Dialed from Germany|001-541-754-3010
http://www.myurl.com?Dialed from France|191 541 754 3010
http://www.myurl.com?skype|155555-SKYPE
http://www.myurl.com?an%20escaped%20name|191%20541%20754%203010
Result
MATCH 1
name [21-25] demo
phone [26-37] 15555555555
MATCH 2
name [59-64] Local
phone [65-73] 754-3010
MATCH 3
name [95-103] Domestic
phone [104-118] (541) 754-3010
MATCH 4
name [140-153] International
phone [154-169] +1-541-754-3010
MATCH 5
name [191-207] Dialed in the US
phone [208-222] 1-541-754-3010
MATCH 6
name [244-263] Dialed from Germany
phone [264-280] 001-541-754-3010
MATCH 7
name [302-320] Dialed from France
phone [321-337] 191 541 754 3010
MATCH 8
name [359-364] skype
phone [365-377] 155555-SKYPE
MATCH 9
name [444-463] an%20escaped%20name
phone [464-486] 191%20541%20754%203010
try demo here
if you create that link yourself, I would recommed you to write a common $GET-variable. you won't need any regex.
http://www.myurl.com?demo=15555555555
echo htmlspecialchars($_GET["demo"]);
You can use this regex:
\?(\w+)\|(\d+)
Working demo
MATCH 1
1. [21-25] `demo`
2. [26-37] `15555555555`
I'm using an Asp.Net RegularExpressionValidator to validate phone numbers.
The check is quite basic - a number can be 10 or 11 characters in length, all numeric and starting 01 or 02.
Here's the regex:
^0[12]\d{8,9}$
However, I've recently started working with a 3rd party, who enforce stricter rules. In my opinon it's a bad idea - partly because they don't even publish these rules, and they are subject to change and therefore maintenance across all their partners. However...
I now need to incorporate their additions into my regex, but I'm not sure where to start.
They currently do this using 2 separate regexes in an OR, however I'd like to do this in 1 if possible.
The additional syntax should ensure that for 10 digit phone numbers also adhere to these additional rules - here's their 10 digit syntax.
"^01(204|208|254|276|297|298|363|364|384|386|404|420|460|461|480|488|524|527|562|566|606|629|635|647|659|695|726|744|750|768|827|837|884|900|905|935|946|949|963|995)[0-9]{5}$
Any ideas as to how to achieve this?
Disclaimer: This answer is based on the logic followed by this answer to demonstrate the "virtual" requirements (which we should drop anyways).
Let me explain what is going on:
^0[12]\d{8,9}$ What's going on here ?
^ : match begin of line
0 : match 0
[12] : match 1 or 2
\d{8,9} : match a digit 8 or 9 times
$ : match end of line
^01(204|20...3|995)[0-9]{5}$ What does this big regex do ?
^ : match begin of line
01 : match 01.
(204|20...3|995) : match certain 3 digit combination
[0-9]{5} : match a digit 5 times
$ : match end of line
Well, what if we merged these two in an OR statement ?
^
(?:
01(204|20...3|995)[0-9]{5}
)
|
(?:
0[12]\d{8,9}
)
$
I'll show you why it doesn't make sense.
How many digits does 0[12]\d{8,9} match ? 10 or 11 right ?
Now how many digits does the other regex match ?
01(204|20...3|995)[0-9]{5}
^^ ^-----\/-----^ ^--\/--^
2 + 3 + 5 = 10
Now if we compare the 2 regexes. It's clear that ^0[12]\d{8,9}$ will match all the digits that are valid for the other regex. So why in the world would you combine these 2 ?
To make the problem simpler, say you have regex1: abc, regex2: [a-z]+. What you want is like abc|[a-z]+, but that doesn't make sense since [a-z]+ will match abc, so we can get ride of abc.
On a side note, \d does match more than you think in some languages. Your final regex should be ^0[12][0-9]{8,9}$.
You could merge them with an OR in the regex itself:
^(?:01(204|208|254|276|297|298|363|364|384|386|404|420|460|461|480|488|524|527|562|566|606|629|635|647|659|695|726|744|750|768|827|837|884|900|905|935|946|949|963|995)\d{5}|0[12]\d{9})$
Edited 11 digit regex.
i want to validate my phone number with the regex for following formats.i have googled the things but i could not find the regex for following formats...
079-26408300 / 8200
(079) 26408300
079 264 083 00
9429527462
can anyone please guide me how can i do validate the phone number field for above formats?
I want to validate the phone number for only above formats as right now am using only following regex var phone_pattern = /^[a-z0-9]+$/i;
#Ali Shah Ahmed
var phone_pattern = "(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})";
here is the way am checking if its valid
if (!phone_pattern.test(personal_phone))
{
$("#restErrorpersonalphone").html('Please enter valid phone number');
$("#personal_phone").addClass('borderColor');
flag = false;
} else {
$("#restErrorpersonalphone").html('');
$("#personal_phone").removeClass('borderColor');
}
its not working. Am I implementing in wrong way?
lets start with the simplest phone number 9429527462
As this has 10 characters and all are numbers, regex for it could be \d{10}
Now the next phone number 079 264 083 00. Regex for this pattern could be (\d{3}\s){3}\d{2}
First we are expecting a group of 3 digits and a space to repeat thrice (\d{3}\s){3}, this will cover 079 264 083 (space included in it), so left will be the last two characters which are handled using \d{2}
For the phone number (079) 26408300, \(\d{3}\)\s\d{8} regex could be use. The regex first looks for a opening bracket, then three digits inside it, and then the closing bracket. It then looks for a space, and then for 8 digits.
The phone number 079-26408300 / 8200 could be validated using regex \d{3}-\d{8}\s/\s\d{4}. It first looks for 3 digits then a -, then 8 digits followed by a space. Then looks for a / and then a space and then 4 digits.
If you wish to know a single regex for validating all the above patterns, do let me know.
Final combined regex would be:
/(\d{10})|(\d{3}-\d{8}\s\/\s\d{4})|((\d{3}\s){3}\d{2})|(\(\d{3}\)\s\d{8})/
Straightforward solution is simple, use |
String ex = "\\d{3}-\\d{8} / \\d{4}|\\(\\d{3}\\) \\d{8}|...