I need to validate that a string follows these rules:
contains numerals
may optionally contain any number of space characters in any position
may not contain any other kind of character
the first two numerals must be one of the set: 02; 03; 07; 08; 13; 18
and the number of numerals must be exactly 10 unless the first two numerals are 1 and 3, in which case the number of numerals may be 10 or 6.
Essentially these are Australian landline (with area code), free-call and 13 numbers.
Ideally the regex should be as implementation-agnostic as possible.
Examples of valid input:
0299998888
02 99998888
02 9999 8888
02 99 998 888
0299 998 888
0299 998888
131999
131 999
13 19 99
1300123456
1300 123456
1300 123 456
1300 12 34 56
1300 12 34 56
PS. I've checked at least 5 other answers and searched for multiple variations of this question, to no avail.
The nearest I have is:
^(?=\d{10}$)(02|03|04|07|08|13|18)\d+
... however this does not account for spacing and won't accept 6 digit numbers beginning with 13.
Note, in theory, the following is acceptable:
1 3 1999
1 3 1 9 9 9
By this I mean that first pair of numerals may have a space between them (as bad as that looks).
Following are examples of random numbers that should fail:
13145 (not enough numerals)
1300-123-456 (hyphens not permitted)
9999 8888 (not enough numerals)
(02) 9999 8888 (parentheses not permitted)
You can make a separate pattern for 13 in alternation:
^(?:(?=(?:\s*\d\s*){10}$)(?:0\s*[2378]|1\s*[38])|(?=(?:\s*\d\s*){6}$)1\s*3).*
Demo: https://regex101.com/r/Hkjus2/2
Related
Random Text
qty 2 MBC102 Rs. 1,890
required 2unit MBC 103
mbc 104 2pcs #5000
MBC 1011 #4000 4 pc Price 5000
3pcs MBC1012 100rolls
MBC1013 500 pc
MBC1014 2pcs mbc 1015
qty 2 # 20000 unit 2
#900 MbC-1016 rolls 150
5000Rs mbc909 mbC 890
56 qty # 5000
mbC 820 qty 90 #25000
Want to match quantity
2
2
2
4
3
100
500
2
2
150
56
90
I tried this code but not work properly
(?i)^(?!qty|unit|pc|pcs|roll|rolls).[0-9]+
The units are after the number, not before. So use two alternatives in the regexp. One alternative has qty as a lookbehind, the other has all the units as a lookahead.
(?i)(?:(?<=^qty\s)\d+|(?:\d+(?=\s?(?:qty|unit|pc|pcs|roll|rolls))))
A single spaces is required after qty when it's before the number, because lookbehinds have to be fixed length. The space is optional when the unit is after the number, so it will match 2pcs and 500 pc
How do I create a regular expression for this rule?
I should only accept this number if these rules are followed.
Starts with 03 or 04, 10 digits long
Starts with 3 or 4, 9 digits long
Edit:
So far, I have only this.
/^[0-9]{9,10}$/
It can only accept numbers with 9 or 10 digits. The starting digits however are still from 0 to 9. It should only be specific, 3 or 4 for 9 digit number, 03 or 04 for 10 digit number. The rule should be one line, applicable to both rules.
This should work
/^0?[34][0-9]{8}$/
You can try below regex:
^(03|04)[1-9]{8}$ -> for Starting by 03 or 04 , 10 digits long
Demo Here
/^(3|4)[0-9]{8}$/gm -> Starting by 3 or 4 , 9 digits long
Demo Here
I have this number: 003859389453604802410207622210986832370060. In this instance, I need to extract 07622210986832 which comes before 02 and ends with 37.
In the real world, 07622210986832 is always 14 digits, and will always start with 02 and end with 37 BUT it could appear at any point in a string that is of random length - all we know is that the number will be there somewhere.
I'm currently using the formula:
=IF(LEN(IFERROR(REGEXEXTRACT(A1:A&"", "02(.*)37")))=14,
However, you will notice in the number sample there is another 02 - "024102".
This is causing an issue.
What I really want to happen is:
Lookup 02
Find the string of 14 numbers and if number 15 is 3 and 16 is 7 (37), that is the number we need.
If you find another 02 number with a 14 digit string and the next two numbers are not 37 - ignore.
Use the pattern 02(\d{14})37, it will extract a sequence of 14 digits preceded by 02 and followed by 37.
try like this:
=ARRAYFORMULA(REGEXEXTRACT(TO_TEXT({A2:A,B2:B,C2:C}), "02(\d{14})37"))
if you want to smash it into 1 column then:
=ARRAYFORMULA(TRIM(TRANSPOSE(QUERY(TRANSPOSE(REGEXEXTRACT(TO_TEXT({A2:A,B2:B,C2:C}),
"02(\d{14})37")),,999^99))))
I have a bunch of numbers which I want to parse.
+79261234567
89261234567
79261234567
9261234567
+7 926 123 45 67
8(926)123-45-67
123-45-67
79261234567
(495)1234567
(495) 123 45 67
89261234567
8-926-123-45-67
8 927 1234 234
8 927 12 12 888
8 927 12 555 12
8 927 123 8 123
What I came with at first is cycle through all the variants like this
(\+[\d]{11}|[\d]{10,11}|\+\d\ [\d]{3}\ [\d]{3}\ [\d]{2}\ [\d]{2}|\d\([\d]{3}\)[\d\-]{9}|[\d\ ]{14,15}|[\d\-]{14,15}|[\d\-]{9}|\(\d\d\d\)[\d\-]{9,10}|\(\d\d\d\)[\d\ ]{9,10}|\(\d\d\d\)[\d\-]{7})
Is there more elegant way to match these numbers?
This regex will match all of the examples and not much extra:
[+]?(\b\d{1,2}[ -]?)?([(]?\d{3}[)]?)((?:[ -]?\d){4,7})(?![ -]?\d)
It can contain between 7 to 12 digits.
Although it would still match with something like this :
+12 (345) 6-7-8 9-0-1
But that should be within acceptable limits.
However, that one could still match part of a longer number.
And to avoid that it would need some negative look-behinds.
(note that there are no look-behinds in javascript regex)
[+]?(?<!\d)(?<!\d[ -])(?:((\d{1,2}[ -]?)?[(]?\d{3}[)]?[ -]?)(\d(?:[ -]?\d){3,6}))(?![ -]?\d)
Here's a regex101 test for that last one.
To have a more elegant solution, you will have to make the pattern more relaxed. One option is to capture 7, 10, or 11 numbers separated by 0 or more delimiters:
\+?(?:[ ()-]*\d){10,11}|(?:[ ()-]*\d){7}
Regex101 Tested
In an application I have the need to validate a string entered by the user.
One number
OR
a range (two numbers separated by a '-')
OR
a list of comma separated numbers and/or ranges
AND
any number must be between 1 and 999999.
A space is allowed before and after a comma and or '-'.
I thought the following regular expression would do it.
(\d{1,6}\040?(,|-)?\040?){1,}
This matches the following (which is excellent). (\040 in the regular expression is the character for space).
00001
12
20,21,22
100-200
1,2-9,11-12
20, 21, 22
100 - 200
1, 2 - 9, 11 - 12
However, I also get a match on:
!!!12
What am I missing here?
You need to anchor your regex
^(\d{1,6}\040?(,|-)?\040?){1,}$
otherwise you will get a partial match on "!!!12", it matches only on the last digits.
See it here on Regexr
/\d*[-]?\d*/
i have tested this with perl:
> cat temp
00001
12
20,21,22
100-200
1,2-9,11-12
20, 21, 22
100-200
1, 2-9, 11-12
> perl -lne 'push #a,/\d*[-]?\d*/g;END{print "#a"}' temp
00001 12 20 21 22 100-200 1 2-9 11-12 20 21 22 100-200 1 2-9 11-12
As the result above shows putting all the regex matches in an array and finally printing the array elements.