I have some data that comes in as a String, and I need to extract or print out the monthvalue ( middle group) that is in the form:
[itemvalue] [monthvalue] [yearvalue]
The rules are:
itemvalue = can be 1-3 characters (or digits) in length
monthvalue = is single alpha character [a-z]
yearvalue = can be 1, 2, or 4 digits representing calender year
Some Example Inputs:
Input1
AP18
Output1
P
Input2
QZAB19
Output2
B
Input3
ARM8
Output3
M
I was trying to compile a pattern like:
Pattern pattern = Pattern.compile("([a-zA-Z0-9]{1,3})([a-z])([0-9]{1,4})");
and then call matcher on the input to find() the groups, in this case, the monthvalue, which should be matcher.group(2) like:
Matcher m = pattern.matcher("OneOfTheExampleInputStringsFromAbove");
if (matcher.find()) {
System.out.println(matcher.group(2));
}
I thought I was close but one issue was how to include a length of 1, 2 and 4, but exclude 3 length for the yearvalue. Is my approach good? Am I missing anything in my Compile pattern?
please let me know!