Q:

need a regex to get string after 4 occurrences of character from end of string in java

Need a regex which will take string after 4 occurrences of a particular character from a string from end of string.
String test = "012.345.Sub_16462_52034247_20151130_.xlsx";

Here i need only 16462_52034247_20151130 from test string.
Trying to get 16462_52034247_20151130_.xlsx so that i can filter out content before dot. In my case that particular charatcer is "_"

A:

I am writing this answer assuming I understood your requirements correctly. Please correct me if I'm wrong.
This answer provides a function to extract the sub-string out of a string based on the following facts:

The function should take two inputs i.e. the original String and a search character.
Start Index of sub-string should be from the 4th last occurrence of the given search character (say "_").

Here is the code snippet:
    public static void main (String[] args)
    {
        /* Function Call */
        System.out.println(getSubstring("012.345.Sub_16462_52034247_20151130_.xlsx",'_'));
    }

    /**
      * Get Sub-String Function
      * Arguments: Original String, Search Char
      * Output: Filtered Sub-String
    **/

    public static String getSubstring(String originalString, char searchChar) {
        /* Char Occurrence */
        int occurrence = 4;
        /* Last Position */
        int position = originalString.lastIndexOf(searchChar);
        /* Search nth Last Occurrence */
        while (occurrence -- > 1 && originalString.lastIndexOf(searchChar, position-1) != -1)
            position = originalString.lastIndexOf(searchChar, position-1);
        /* Add Additional Filter & Return Sub-String */
        return originalString.substring(position+1,originalString.lastIndexOf('.') - 1);
    }

Output:
16462_52034247_20151130

