Regular expression for barcodes (12 or 14 digits) - regex

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

Related

C++ multiple regex conditions syntax

In other words, is there an AND operator in c++ regex? Normally I would just use | but it doesn't work
For example I want to return only 2 and 1 digit numbers
string subject("This 91 - 500abc7 is a 5 test");
regex re("\\d\\d");
This only returns 2 digit numbers, how do I add a second condition to also match single digits "\d"
Result should be:
91 - 7 - 5
It is not a "and" you want to have 1 OR 2 digits (\d|\d\d).
but regex have notation for numbered repetition: \d{1,2}
Issue is that \d{1,2} would match 50 in 500.
So you might add (negative) look ahead/behind:
(?<!\d)\d{1,2}(?!\d) (1 or 2 digits not preceded and followed by another digit)
so std::regex re(R"((?<!\d)\d{1,2}(?!\d))");

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}

How to recreate this reg expression to fit more conditions?

Regular expression:
/(\b|[x])\d{7}\b/
Essentially what I'm trying to do is only highlight the digits and not the letter "x". I also would like to highlight the 7 digits even if it has multiple spacse between the main number and the 7 digits. I seem to be only be able to highlight expressions when its followed by exactly 1 space.
Thanks!
So you want to highlight exactly 7 digits that are followed by space or x?
Use this pattern.
(?<=[x ])\d{7}(?!\d)
(?<=[x ]) is lookbehind. means only match 7 digits when the preceding character is x or whitespace.
(?!\d). is negated lookahead. should be exact 7 digits. so the next character after 7 digits should not be digit.

What does this regular expression mean?

could somebody please tell me what the following expressions means:
\d{6,8}
As far as I know it's a regular exp
Between 6 and 8 numeric digits.
(As it's not anchored to boundaries or start & end of string, it would also match between 6 and 8 digits within a longer series of digits - for instance, it will match 123456, 1234567, 1234678, but also the first 8 digits of 123456789.)
\d is a character class - it could also have been written as [0-9]. The {} part is a repetition count; it could be a single number, e.g. {6}, or, as in this case, a range - so the {6,8} means "the previous thing, repeated between 6 and 8 times".
it matches between 6 and 8 sequential numeric digits.
\d is equivalent to the character class [0-9], and the {,} notation specifies an exact number of times that a pattern has to match.
matches a digit that is of length between 6 and 8
it means, at least 6 digits and no more than 8 digits
It means between 6 to 8 numbers in a row.
\d means a number [0-9]
{6, 8} means min. of 6, max. of 8
You use curly braces to describe how many of the previous character you want to look for. Entering a single number, like {3} means 3 in a row. Adding a second number changes this into min/max.
http://www.regular-expressions.info
is the best site on the web for learning about regular expressions.

How to match a 9 or 14 digits long number with regex?

I need to check by Regex expression if 9 or 14 digits are typed.
The expression \d{9}|\d{14} seems to be not working properly, what's wrong ?
This regex should work.
^(\d{9}|\d{14})$
Could you post the piece of code you're using and tell us what language are you using?
If you're using regex chances are that you have a string, and I'm sure your language has something to count string length.
EDIT:
as Rubens Farias pointed out in comments maybe ^...$ is needed because your regex would match any number with more than 9 digit as that number has a substring with a 9 digit long number.
Anyway check if you can do it with your language's string's methods/functions
You can use:
^(?:\d{9}|\d{14})$
Explanation:
^ - Start anchor
(?: - Start of non-capturing group
\d{9} - 9 digits
| - alternation
\d{14} - 14 digits
) - close of the group.
$ - End anchor
alternatively you can do:
^\d{9}(?:\d{5})?$
which matches 9 digits followed by optional 5 digits.
(\d{9}|\d{14}) should work