Regex for this rule - regex

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

Related

Extract fixed length string between two numbers

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))))

Regex for 10 digit phone number with variable spacing

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

RegEx to allow 12 or 13 digits

In my MVC application I define validation using the following RegEx
[RegularExpression(#"\d{8}0[1-2]\d{3}", ErrorMessage = "Must be numeric, 12 or 13 characters long & Format xxxxxxxx[01 or 02]xxx")]
But I want to allow 12 or 13 characters. The d{3} appears to be forcing that overall I have 13 characters input
To allow it to accept 12 or 13, I have changed d{3} to d{2} and its accepting 12 now.
But - can I be sure it will still take 13 characters?
Must be numeric, 12 or 13 characters long & Format xxxxxxxx[01 or 02]xxx
To allow digits 1 or 2 after first nine digits,
^\d{8}0[12]\d{2,3}$
^^^^ : Allow 1 or 2 after `0`
^^^^^^^ : Any two or three digits
Note that [12] can also be written as (1|2) using OR/alteration.
Demo

How to add zero in front of single digit values using REGEX in pentaho

I have the month values in a flat file like
Month
12
11
1
2
8
10
now i want to add zero in front of single digit values & double digit as same.
output should be like
Month
12
11
01
02
08
10
This am doing in PENTAHO (I will implement in Replace in string step)
I am not aware of PENTAHO. But following regex should work for most of the languages
Match : \b([0-9])\b
Replace : 0$1
regex101 demo

Regular expression for matching numbers and ranges of numbers

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.