regular expression to identify certain number pattern - regex

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}

Related

regex to extract a specific set of numbers

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

Regex Match Hexidecimal in Groups of 2-8

I am working on a regular expression to match against a hexadecimal string and having some trouble near the end. I am specifically looking for groups of 2 bytes that do not contain 00 that are between 2 and 8 bytes long. I have it all working except that when there are less than 8 bytes, it will allow extra 00 to be in it sometimes.
https://regex101.com/r/jq3QpP/1/
(?!(00)+)([0-9a-fA-F]{2,8})?(?!(00)+) // This on the below text gives the following matches
C86B0200554E0200C86B02000000000000000000270000008109000000000000EC6A050079750
18881000000410000280100000000000000000001000002010400000000000000000000000000
0000000000000000000000F65FA45900000000FF0000002F0000000000000049000000403C9F5
A000000000000000000000000FFFF330000000000000F06EAE8333536
Match 1
Full match 0-8 `C86B0200`
Group 2. 0-8 `C86B0200`
Match 2
Full match 8-16 `554E0200`
Group 2. 8-16 `554E0200`
Match 3
Full match 16-21 `C86B0`
Group 2. 16-21 `C86B0`
Match 4
Full match 21-21 ``
Match 5
Full match 39-47 `02700000`
Group 2. 39-47 `02700000`
In match 1,2,5 there are extra 00, in match 3, it missed the 20 for some reason. If you have an idea what I missed, please let me know
You can avoid matching 00 by allowing only one 0 in two digits at a time instead:
(?:[A-F1-9][A-F0-9]|[A-F0-9][A-F1-9]){1,4}(?=(?:..)*$)
Demo: https://regex101.com/r/2hebvf/2

Regular Expression for Number Less Then 60

I'm looking for a regular expression for any number from 1-60, here's what I have so far:
[0-6][0-9]
Super simple. When I input a number like 950, it catches the 50, ignores a 9 is in front.
Thanks in advance.
\b([1-9]|[1-5]\d|60)\b
You have to encapsulate it in word breaks (\b) to ensure parts of a number aren't matched (like 950).
The idea is to either match a single digit number - 1 to 9 inclusive ([1-9]), or (|) a two digit number, where the first digit is between 1 and 5 inclusive ([1-5]\d), or 60 itself.

Regex: how to only allow integers greater than zero

I have tried the following to only allow integers in my text box, this works great but it allows a zero in there. Is there anything else I can add to prevent a zero being added?
\d+
This will allow 10 but not 01, and it will allow only numbers consisting of digits, i.e., no periods or minus signs...but also no plus signs, scientific notation etc.
^[1-9][0-9]*$
A minor variation is this:
/\d*[1-9]\d*/
That would allow leading zeros.
If you are not concerned about negatives and silly numbers like 07, this will do:
/[1-9]\d*/
For a more robust solution, I suggest converting the matched string to integer and check if it fulfills your criteria.
Code:
^([1-9][0-9]+|[1-9])$
Example: http://regexr.com/3annd
Tested with:
0
10
01
11
00
1
100
^(0*[1-9][0-9]*)$
This will allow "silly" numbers like 007 as well, but not 0 or 000 or an empty string.
Note that \d matches also digits from other character sets like ٠١٢٣٤٥٦٧٨٩. See: \d is less efficient than [0-9].
^ denotes the start, $ the end of the string. Together they ensure that the whole string is matched.
^(\d{2}[1-9])$
matches with:
from 001 to 999
example
001
099
999
does not match
000
01
0

Regular expression for barcodes (12 or 14 digits)

I'm looking for regular expression which will match barcode. The barcodes can be 12 or 14 digits long (never 13) and the first digit has to be 8. Examples:
800909887898
80903243234323
I've got something like this: ^[8]{1}[0-9]{11}$ but I don't know hot to recognize last 2 digits. It's can be 2 digits character or null (the expression [0-9]{0-2} will not work fine because quantifier don't work like OR).
Check this one
^8[0-9]{11}([0-9]{2})?$
[0-9]{2} means two digits
(...)? means zero or one time
So, if you want something that starts with 8 and can be exactly 12 or 14 digits, then you can use | for an "or" in the regex:
^8\d{11}$|^8\d{13}$