Mickey Mickey - 1 year ago 96
Android Question

Java REGEX Pattern Problems

I'm working on a call record app ,I had a problem that my pattern was not matching but i solved that.Still the solution is not the best.

String subject = "d20160812215452p38762620479tincomingmfalse.3gp";

String pattern = "^d[\\d]{14}p[\\d]{11}t[\\w]{8}m[\\w]*\\.3gp";

System.out.print("Not maching");

The problem is that the phone number
String subject****p38762620479****

might be bigger than 8 characters ,also how could i split the pattern that it shows: the date,number.....

Answer Source

Adding to @kolboc's answer, in order to get the groups you matched, do this:

String subject = "d20160812215452p38762620479tincomingmfalse.3gp";
String pattern = "^d([\\d]{14})p([\\d]{11})t([\\w]{8}m[\\w]*\\.3gp)";
Pattern r = Pattern.compile(pattern);

// create a matcher object
Matcher m = r.matcher(subject);
if (m.find( )) {
   System.out.println("Found all: " + m.group(0) );
   System.out.println("Found date: " + m.group(1) );
   System.out.println("Found phone: " + m.group(2) );
   System.out.println("Found title: " + m.group(3) );
} else {
   System.out.println("NO MATCH");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download