iCode iCode - 2 months ago 11
Java Question

Find string after last underscore before dot extension

I need to find

20140809T0000Z
in this string:

PREVIMER_F2-MARS3D-MENOR1200_20140809T0000Z.nc


I tried the following to keep the string before the
.nc
:

(?<=_)(.*)(?=.nc)


I have the following to start from the last underscore:

/_[^_]*$/


How can I find string after last underscore before dot extension, using a regex?

Answer

RegEx is not always the best solution... :)

String pattern="PREVIMER_F2-MARS3D-MENOR1200_20140809T0000Z.nc";
int start=pattern.lastIndexOf("_") + 1;
int end=pattern.lastIndexOf(".");
if(start != 0 && end != -1 && end > start) {
     System.out.println(pattern.substring(start,end);
}
Comments