Reputation: 786
I have the following barcode that I need to validate via regex:
TE1310 2000183B 804F58000020183B 20120509 0013.0002.0000 20161201
We're having an issue with our barcode scanners occassionally cutting off some characters from barcodes, so I need to validate it via the following regex rules:
I have the following regex & I'm pretty close but not sure how to do #7 above (0013.0002.0000). I placed "????" into my regex below where I'm unsure of how to do this part:
TE1310\s[A-Za-z0-9]{8}\s[A-Za-z0-9]{16}\s????\s\d{8}
Any idea how to do this? Thanks
Upvotes: 1
Views: 2846
Reputation: 179046
I'm assuming a regular expression syntax similar to JavaScript, the basic ideas can be converted into any other regex that I know of.
^TE1310
^
is used to match only at the beginning of a string, the characters that follow are matched literally.
/^TE1310 /
I'm adding the /
regex delimiters to show that there is in fact a space character contained within the regex. If your regex syntax supports alternative delimiters, you might see something along the lines of ~^TE1310 ~
instead.
/^TE1310 [a-zA-Z0-9]{8}/
[abc]
is used to select a character in the provided set, the use of a-zA-Z0-9
is to match any letter (upper or lower case) or number.
{n}
is used to repeat the previous selector n
times.
/^TE1310 [a-zA-Z0-9]{8} /
/^TE1310 [a-zA-Z0-9]{8} [a-zA-Z0-9]{16}/
/^TE1310 [a-zA-Z0-9]{8} [a-zA-Z0-9]{16} /
0013.0002.0000
/^TE1310 [a-zA-Z0-9]{8} [a-zA-Z0-9]{16} 0013\.0002\.0000/
\.
is used to escape the .
which is a selector for any non-newline character. If you're building the Regex in a string, you may need to double escape the \
character, so it may be \\.
instead of \.
/^TE1310 [a-zA-Z0-9]{8} [a-zA-Z0-9]{16} 0013\.0002\.0000 /
/^TE1310 [a-zA-Z0-9]{8} [a-zA-Z0-9]{16} 0013\.0002\.0000 \d{8}/
\d
matches numbers, it's equivalent to [0-9]
. Similarly to \.
you may need to double escape the \
character, which would be \\d
instead.
You didn't mention it explicitly, but I assume the match should only match lines that exactly match this pattern, and aren't followed by trailing numbers/letters:
/^TE1310 [a-zA-Z0-9]{8} [a-zA-Z0-9]{16} 0013\.0002\.0000 \d{8}$/
$
is used to match the very end of the string.
Upvotes: 4
Reputation: 31194
#7
is trivial, it should be simply 0013\.0002\.0000
you have to make sure to escape your periods, and escape your escape characters if that's what the language requires
So, try
TE1310\s[A-Za-z0-9]{8}\s[A-Za-z0-9]{16}\s0013\.0002\.0000\s\d{8}
assuming the rest of the points are correct, of course.
Also, as Sednus
said, you might want to match the beginning and end of the string. the conventional symbols are ^
for beginning and $
for the end, but I'd check a reference for your particular language just in case.
If you don't do that, the regex will find any TE1310 2000183B 804F58000020183B 20120509 0013.0002.0000 20161201
in a larger string, such as
asgsdaTE1310 2000183B 804F58000020183B 20120509 0013.0002.0000 20161201qeasdfa
Upvotes: 2