How I can write a regex which accepts 10 or 14 digits separated by a single space in groups of 1,2 or 3 digits?
examples:
123 45 6 789 1 is valid
1234 567 8 9 1 is not valid (group of 4 digits)
123 45 6 789 109 123 8374 is not valid (not 10 or 14 digits)
EDIT
This is what I have tried so far
[0-9 ]{10,14}+
But it validates also 11,12,13 numbers, and doesn't check for group of numbers
You may use this regex with lookahead assertion:
^(?=(?:\d ?){10}(?:(?:\d ?){4})?$)\d{1,3}(?: \d{1,3})+$
RegEx Demo
Here (?=...) is lookahead assertion that enforces presence of 10 or 14 digits in input.
\d{1,3}(?: \d{1,3})+ matches input with 1 to 3 digits separated by space with no space allowed at start or end.
aggtr,
You can match your use case with the following:
^(?:\d\s?){10}$|^(?:\d\s?){14}$
^ means the beginning of the string and $ means the end of the string.
(?:...) means a non-capturing group. Thus, the part before the | means a string that starts and has a non-capturing group of a decimal followed by an optional space that has exactly 10 items followed by the end of the string. By putting the | you allow for either 10 or 14 of your pattern.
Edit I missed the part of your requirement to have the digits grouped by 1, 2, or 3 digits.
Related
I need a regular expression to extract a specific set of numbers from a string. The string could contain letters, special characters and spaces.
Input examples:
This is a test 99 12 3456
This is test 2 94123456
This is test 3 357 95123456
This is test 4 35797123 456
And so on…
The regex should look for a string of 8 numbers starting with 94 or 95 or 96 or 97 or 99 followed by 6 more numbers.
example:
94<6 more numbers here>
95<6 more numbers here>
96<6 more numbers here>
97<6 more numbers here>
99<6 more numbers here>
or 11 numbers starting with 357 followed by 94 or 95 or 96 or 97 and 6 more numbers.
example:
35794<6 more numbers here>
35795<6 more numbers here>
35796<6 more numbers here>
35797<6 more numbers here>
35799<6 more numbers here>
So the output should either be 8 numbers, or 11 numbers. Less than 8 or more than 11 is not a valid output. Also anything between 8 and 11 is not valid.
Hope this makes it more clear
Thanks for your help
Maybe this:
(357|94|95|96)[\d ]{6,}
Which means "357" or "94" or "95" or "96" followed by at least six digits and/or spaces. I wasn't sure exactly what you want. It would be better just to post the exact input and output desired.
If you’re working in an environment that supports lookbehinds, you can ensure you’re not matching a partial number by using a negative lookbehind and negative lookahead:
/(?<!\d)(?:357)?9[4-79]\d{6}(?!\d)/
(?<!\d): Negative lookbehind (ensure there isn’t a digit before the matching expression)
(?:357)?: Create a non-capturing group of 357 to attach an optional quantifier (match 357 zero to 1 times)
9: Match 9
[4-79]: Character set with range 4-7 and 9 (match one of these characters)
\d{6}: Match a digit exactly 6 times
(?!\d): Negative lookahead (ensure there isn’t a digit after the matching expression)
This regular expression will do it if you remove the spaces from the input first: 3579[4-9](?:\d{8}|\d{6})
I am trying to do regex validation for 11 digit mobile number of type 03025398448.Where first 3 digits are constant 030 and remaining 8 digits are from 0 to 9 (any number) and 1st digit could be written in +92 format .So, help me for this number regex code
If the number should start with 030 and +92 is optional and when using +92 you should omit the leading zero, you could use:
^(?:\+9230|030)?\d{8}$
Explanation
^ # From the beginning of the string
(?: # Non capturing group
\+9230|030 # Match +9230 or 030
)? # close capturing group and make it optional
\d{8} # Match 8 digits
$ # The end of the string
In C# you could use this as string pattern = #"^(?:\+9230|030)?\d{8}$";
C# code
You can use this regular expression:
^((\+?92)30[0-9]{8}|030[0-9]{8})$
Explanation
BeginOfLine
CapturingGroup
GroupNumber:1
OR: match either of the followings
Sequence: match all of the followings in order
CapturingGroup
GroupNumber:2
Sequence: match all of the followings in order
Repeat
+
optional
9 2
3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
Sequence: match all of the followings in order
0 3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
EndOfLine
I have to generate a regular expression to detect patterns of text where credit card numbers are involved, I have a regular expression but fails when the text is altered with simple spaces between the text for example (not valid credit card number):
4320 7589 9456 0123
The regex is:
4\d{3}(\s+|-)?\d{4}(\s+|-)?\d{4}(\s+|-)?\d{4}
This regex match easy, but if someone alter the text with spaces between any number like this:
4 320 7589 9456 0123
Does not match, I need a regex to detect any possible variable with spaces, special symbols, letters, some examples:
43 20 75 89 94 56 01 23
4 3 2 0 7 5 8 9 9 4 5 6 0 1 2 3
4320a7589b9456c0123
4320$7589$9456$0123
4320_7589_9456_0123
I don't know if I can strip any space, symbols from the pattern to analyze the text?
I am posting because you actually asked for help with pattern to match any number of non-digits between the first 4 and 15 more digits.
The pattern is
^4(?:\D*\d){15}$
See demo
Regex breakdown:
^ - start of string
4 - literal 4
(?:\D*\d){15} - 15 occurrences of sequences of...
\D* - 0 or more non-digit symbols before..
\d - a digit
$ - end of string
If you need to capture, you can capture (like ^4((?:\D*\d){3})((?:\D*\d){4})((?:\D*\d){4})((?:\D*\d){4})$), but the submatches will still contain the "junk" in-between digits.
I have a text file which does not have any specific format. It contains text and numbers. I want to get numbers only with 24 digits. I want to remove all extra text and get those numbers separated by space or newline.
I can select numbers with 24 digits by using [0-9]{24} but I want to remove all extra text and leave the numbers there.
For example, if the file is like this:
asafa sfasd asd 123 15 1 asd ad7a sd78a6s da87ds6a 8s7d .123 1.
32 141.23 . 123456789012345678901234 asafa sfasd asd 123 15 1 asd ad7a sd78a6s da87ds6
a 8s7d .123 1.32 141.23 . 123456789012345678901234 asafa sfasd asd 123 15 1 asd ad7a sd78a6s da87ds6a 8s7d .12
3 1.32 141.23 . 123456789012345678901234 asafa sfasd asd 123 15 1 asd ad7a sd78a6s da87ds6a 8s7d .123 1.32 141.23 . 123456789012345678901234
I want to get
123456789012345678901234 123456789012345678901234 123456789012345678901234 123456789012345678901234
separated by space or newline (any separator would do.) Numbers are not always the same in the file, this is just an example to show what I'm going to do.
Thanks.
You might use the following regex and replace with an empty string:
(?>(?:\D|(?<!\d)\d{1,23}(?!\d)|(?<!\d)\d{25,}(?!\d))+)
It will match all text that is not digits, or numbers that are not 24 symbols long.
Settings screen:
REGEX EXPLANATION:
(?>...) - An atomic group syntax, we do not backtrack inside the group (it increases performance)
(?:\D|(?<!\d)\d{1,23}(?!\d)|(?<!\d)\d{25,}(?!\d))+ - A non-capturing group where we list our alternatives (the patterns we want to match) that are listed with | alternation operator:
\D - a non-digit
(?<!\d)\d{1,23}(?!\d) - Any sequence of 1 to 23 digits that are not preceded with a digit (thanks to the negative look-behind (?<!\d)), and are not followed by a digit (thanks to the negative look-ahead (?!\d))
(?<!\d)\d{25,}(?!\d) - A similar to the above, but it matches sequences of 25 digits and more.
I'm trying to get a regex to validate international phone numbers.
This is what i have so far:
^\+?( |\(|\))*?\d{8,14}$
What I'm aiming for is total allowed digits is 8-14. In addition, to allow as many whitespace and dashes in between, after or before the digits.
Examples:
12345678901234
123 456789 0 1 2 3 4
+1 2345678901234
(12) 3 4 56 7 8 9 01234
1-2-3-4-5-6-7 8 9 0 123 4
I'm not sure how to go about this.
Thanks.
The below regex would help you.
^(?=(?:\D*\d\D*){8,14}$)[ -]*\+?[ -]*(?:\((?:[ -]*\d)+[ -]*\))?[- \d]*
DEMO
OR
^(?=(?:\D*\d\D*){8,14}$)[- \d()+]*
DEMO
\D matches any non-digit character. (?=(?:\D*\d\D*){8,14}$) asserts that the string we are going to match must contain 8 to 14 digit.