Q:

Regular expression get the third element from a string

Hello Im having trouble getting the third element of a string (F604080)
<sourceDocumentId>AX02_APF604_F604080</sourceDocumentId>

I have tried with this regular expression and variations, but i can manage to get 
F604080.
(?<=\w+_)\w+(?=\<)

(?<=\w+_\w+_)\w+(?=\<)

....
Any help will be appreciated.
Thanks.

A:

You don't need look behind or look ahead, instead just use this simple regex,
.*_(\w+)

and capture group 1.
Java codes,
public static void main(String[] args) {
    String s = "<sourceDocumentId>AX02_APF604_F604080</sourceDocumentId>";
    Pattern p = Pattern.compile(".*_(\\w+)");
    Matcher m = p.matcher(s);

    if (m.find()) {
        System.out.println(m.group(1));
    } else {
        System.out.println("Didn't match");
    }
}

Prints this like you wanted.
F604080

