regex to extract a specific set of numbers - regex

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

Related

RegEx for decimal number with maximum length ignoring dot

I am after a regex that allows a number to be a maximum length with decimal places.
It should allow 16 numbers maximum and 3 decimal places, however the 16 max should include the decimals but not the . character. I've tried and have this so far: ^(?=^[\d\.].{0,16}$)[0-9]+(\.[0-9]{1,3})?$, which is close except it allows 17 single numbers, when it should only accept 16. If I change {0,16} to {0,15} it then breaks the decimals.
Accepted values:
123
123.3
123.45
123.456
1111111111111.345 (totally 16 numbers)
11111111111111.45 (totally 16 numbers)
Rejected:
123.1234
11111111111111111 (17 characters)
1111111111111111.
111111111111111.56 (15 characters and 2 numbers = 17)
Thanks in advance.
Your regex is fairly close, you may use this regex:
^(?=(?:\d\.?){0,16}$)\d+(?:\.\d{1,3})?$
RegEx Demo
RegEx Details:
(?=(?:\d\.?){0,16}$): is positive lookahead to assert that we have 0 to 16 digits in input where dot is optional.
\d+(?:\.\d{1,3})?: Match an integer or a decimal number with 1 to 3 digits.

regex: Numbers and spaces (10 or 14 numbers)

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.

regular expression to identify certain number pattern

I want to build a regular expression to identify certain number pattern
The expressions required would be:
1)
6 numbers, starting with 00
2)
6 numbers, starting with 01
3)
8 numbers, starting with 200.
I started with ^\d{0,6}(.\d{00})?$ bit it did not work
How can it be done?
Try this:
^(0[01][0-9]{4}|200[0-9]{5})$
Will match 0 followed by a 0 or 1 followed by 4 numbers 0-9 (total 6 digits), or it will match 200 followed by 5 digits (total 8 digits)
(Using character groups, due to the fact that the language was not specified, therefore, whether the special characters need extra escapes is unknown)
I think you are looking for the alternation operator |. It takes either the pattern to the left or the pattern to the right. Hence, you end up with the following regular expression:
^(00\d{4}|01\d{4}|200\d{5})$
How about this: a number starting with either 200 and a number, 00 or 01 and 4 numbers
(200\d|00|01)\d{4}

PCI Compliance regex detect pattern with spaces

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.

find numbers with fix length in text file

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.