import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\|poke\\|p([1,2])\\|(.+?)(,|\\|)";
final String string = "|poke|p1|Volcarona, M|\n"
+ "|poke|p1|Urshifu-*, F|\n"
+ "|poke|p1|Magnezone|\n"
+ "|poke|p1|Tangrowth, M|\n"
+ "|poke|p1|Corviknight, F|\n"
+ "|poke|p1|Hippowdon, F|\n"
+ "|poke|p2|Togekiss, M|\n"
+ "|poke|p2|Kommo-o, M|\n"
+ "|poke|p2|Jirachi|\n"
+ "|poke|p2|Mimikyu, F|\n"
+ "|poke|p2|Dragalge, F|\n"
+ "|poke|p1|Dragapult, F|\n"
+ "|poke|p1|Hatterene, F|\n"
+ "|poke|p1|Flygon, F|\n"
+ "|poke|p1|Inteleon, F|\n"
+ "|poke|p1|Obstagoon, F|\n"
+ "|poke|p1|Arcanine, F|\n"
+ "|poke|p2|Pincurchin, F|\n"
+ "|poke|p2|Magnezone|\n"
+ "|poke|p2|Hawlucha, F|\n"
+ "|poke|p2|Raichu-Alola, M|\n"
+ "|poke|p2|Ferrothorn, M|\n"
+ "|poke|p2|Magearna-Original|\n"
+ "|poke|p1|Slowbro, M|\n"
+ "|poke|p1|Tangrowth, F|\n"
+ "|poke|p1|Crawdaunt, F|\n"
+ "|poke|p1|Dragapult, M|\n"
+ "|poke|p1|Clefable, M|\n"
+ "|poke|p1|Excadrill, M|\n"
+ "|poke|p2|Togekiss, F|\n"
+ "|poke|p2|Starmie|\n"
+ "|poke|p2|Urshifu-*, M|\n"
+ "|poke|p2|Amoonguss, M|\n"
+ "|poke|p2|Cinderace, F|\n"
+ "|poke|p2|Jirachi|";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html