Reputation: 1
So this is the problem. Can someone tell me how a regex pattern might look like?
Write a program that determines if the input string matches the following format:
Format:
PRODUCT_ID.PRODUCT_CATEGORY-LOCATOR_TYPE[LOCATOR_LOT]
PRODUCT_ID
= always starts with # followed by 3 zeros, followed by a numeric value that can be 1-7 digits, in the range 1-9999999PRODUCT_CATEGORY
= 1-4 uppercase alphabetic charactersLOCATOR_TYPE
= a single uppercase X, Y or Z characterLOCATOR_LOT
= 1-2 numeric digits, in the range 1-99All other format characters are the literal characters
Return true if it matches and false otherwise.
This is the function declaration:
public boolean checkPattern(String s){
}
I tried splitting the string and then checking every character but it got very complicated.
Here's what I've got so far for a regex:
String regex = "#000^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9])$";
This is something I started with but its so long and complicated and not even complete (only checks the product id) that I dont think I am on the right track here
Upvotes: 0
Views: 172
Reputation: 171
I think you should try to determine the pattern of each element and then merge between them. I was able to identify every part but I did not understand the last one:
All other format characters are the literal characters.
Anyway, I hope this will help you out. Read the comments included in the code.
public boolean checkPattern(String s){
if(s.matches("#000[1-9]{1,7}.[A-Z]{1,4}-([X-Z])\\[[1-9]{1,2}\\]")) return true;
else return false;
// #000[1-9]{1,7} ==> matches PRODUCT_ID
//. ==> to simply match the point
// [A-Z]{1,4} ==> PRODUCT_CATEGORY
//- ==> to simply match this symbol
// ([X-Z]) ==> LOCATOR_TYPE
// \\[ Use the double slash here so the [ symbol becomes recognized
// [1-9]{1,2} ==> LOCATOR_LOT
// \\]
// String s="#000123.ABC-X[99]"; this is a test example I used.
}
While working with Regular expressions, you have to read well what is requested from you as well as the predefined regex operators and tools.
Upvotes: 0
Reputation: 776
Here's a working regex:
#000[1-9]\d{0,6}\.[A-Z]{1,4}\-[XYZ]\[[1-9]\d?\]
And here's a breakdown:
#000 # match the literal characters, #000
[1-9] # any digit 1 to 9 (to ensure there are no preceding zeroes)
\d # any digit character; equivalent to [0-9]
{0,6} # perform the preceding match (\d) anywhere from 0 to 6 times
# (0,6 instead of 1,7 because we already matched the first digit above)
\. # match a dot character. Must be escaped with a backslash \ as
# the unescaped dot will match *anything* in regex otherwise.
[A-Z] # any uppercase alphabetic character.
{1,4} # will repeat the preceding match anywhere from 1 to 4 times.
\- # match a hyphen character. escaping is optional here.
[XYZ] # any of X, Y, or Z.
\[ # a literal [ character. Must be escaped.
[1-9] # matches 1 to 9
\d # any digit; equivalent to [0-9]
? # Makes the preceding match optional. Equivalent to {0,1}
\] # a literal ] character. Must be escaped.
Other notes:
A really nice tool that can help you understand regex better is the website RegExr.com
Upvotes: 1