import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\[buyer_(?:email|mobile|fullname|address)\\] =>\\s*\\K.*";
final String string = "SimpleXMLElement Object\n"
+ "(\n"
+ "[error_code] => 00\n"
+ "[token] => 1182263526325de72aw828369e7aa\n"
+ "[description] => SimpleXMLElement Object\n"
+ " (\n"
+ " )\n\n"
+ "[transaction_status] => 00\n"
+ "[receiver_email] => myemail@gmail.com\n"
+ "[order_code] => 3212423\n"
+ "[total_amount] => 200\n"
+ "[payment_method] => SimpleXMLElement Object\n"
+ " (\n"
+ " )\n\n"
+ "[bank_code] => NLAZ\n"
+ "[payment_type] => 1\n"
+ "[order_description] => SimpleXMLElement Object\n"
+ " (\n"
+ " )\n\n"
+ "[tax_amount] => 0\n"
+ "[discount_amount] => 0\n"
+ "[fee_shipping] => 0\n"
+ "[return_url] => success.php\n"
+ "[cancel_url] => order.php\n"
+ "[buyer_fullname] => eric phuong\n"
+ "[buyer_email] => eric@domain.com\n"
+ "[buyer_mobile] => 012108609\n"
+ "[buyer_address] => abc\n"
+ "[affiliate_code] => SimpleXMLElement Object\n"
+ " (\n"
+ " )\n\n"
+ "[transaction_id] => 12345\n"
+ ")";
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