File file = new File("file-type-string-i-want-2000-01-01-01-01-01.conf.gz"); Matcher matcher = pattern.compile("\\-(.*)\\-\\d{4}")).matcher(fileName); StringBuilder sb = new StringBuilder(); while (matcher.find()) { sb.append(matcher.group()); } stringList = Arrays.asList(sb.toString().split("-")); if (stringList.size() >= 2) { nameFragment = stringList.get(stringList.size() - 2); }
Desired Result - Extract
string-iwant
from lines that look like
file-type-string-iwant-2000-01-01-01-01-01.conf.gz
Unfortunately, the string-iwant format is the non-fixed length of alphanumeric characters that will include ONE hyphen only, but never begin with a hyphen. The date formatting is consistent, the year is always after the line, so my current approach is matching -year, but I can hardly exclude the material at the beginning.
Thanks for any thoughts or ideas.
Edit: updated rows
source share