Q:

Regular Expressions (RegEx) - One String Variable length

My application receives one barcode where its length is variable. It could be 5, 9, 12 or 14. I'm trying to use something like this:  \w{14}|\w{12}|\w{9}|\w{5}
That regular expression is not correct. Is there any regular expression for different lengths?
or What is the right way to combine different regular expressions?
Note: The reason why I'm using regular expressions is a long story.

A:

Is this what you are looking for?
\b(\w{14}|\w{12}|\w{9}|\w{5})\b

It will match all the words within your string that are either 14, 12, 9, or 5 characters long. You can then check these matches. See a string that contains several matches and non-matches here: http://www.debuggex.com/r/V9T8nHp7ep6yuJow
If you are only hoping to check that the entire string is a single match, use this (to check that it both starts and ends with one of your options):
^(\w{14}|\w{12}|\w{9}|\w{5})$

Also, all the barcodes I've seen consist only of numbers, is this is the case for you as well... you should replace all the \w with \d which is the same as [0-9]

A:

Most likely you wanted this:
"^(\w{14}|\w{12}|\w{9}|\w{5})$"

Notice the grouping of the different length options, surrounded by beginning and ending of string indicators.  Without those, anything matches if \w{5} exists somewhere in the string.

