[Solved] Substring a string from both end in java [duplicate]


You could parse XML or use regex. To keep things simple, I would suggest regex. Here is how you can use it:

Pattern pattern = Pattern.compile("<span id=\"artist\">(.*?)<\\/span><span id=\"titl\">(.*?)<\\/span>");
Matcher m = pattern.matcher(input);
if (m.find() {
    MatchResult result = m.toMatchResult();
    String artist = result.group(1);
    String title = result.group(3);
}

Where input is the XML you have.

In regex, patterns inside parenthesis represent capture groups. They allow you to extract results from a match. However, note that in the code you retrieve the group 1 and 3. That’s because for every capture group you have the own capture group and the result. So, result.group(0) would be the pattern for the first group and result.group(2) would be the pattern for the second group.

solved Substring a string from both end in java [duplicate]