import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:FN:Tom )((.|\\n)*)(?:\\nEND:VCARD)";
final String string = "FN:Tom root:x:0:0:root:/root:/bin/bash\n"
+ "daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin\n"
+ "bin:x:2:2:bin:/bin:/usr/sbin/nologin\n"
+ "sys:x:3:3:sys:/dev:/usr/sbin/nologin\n"
+ "sync:x:4:65534:sync:/bin:/bin/sync\n"
+ "games:x:5:60:games:/usr/games:/usr/sbin/nologin\n"
+ "man:x:6:12:man:/var/cache/man:/usr/sbin/nologin\n"
+ "lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin\n"
+ "mail:x:8:8:mail:/var/mail:/usr/sbin/nologin\n"
+ "news:x:9:9:news:/var/spool/news:/usr/sbin/nologin\n"
+ "uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin\n"
+ "proxy:x:13:13:proxy:/bin:/usr/sbin/nologin\n"
+ "www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin\n"
+ "backup:x:34:34:backup:/var/backups:/usr/sbin/nologin\n"
+ "list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin\n"
+ "irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin\n"
+ "gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin\n"
+ "nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin\n"
+ "systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false\n"
+ "systemd-network:x:101:103:systemd Network Management,,,:/run/systemd/netif:/bin/false\n"
+ "systemd-resolve:x:102:104:systemd Resolver,,,:/run/systemd/resolve:/bin/false\n"
+ "systemd-bus-proxy:x:103:105:systemd Bus Proxy,,,:/run/systemd:/bin/false\n"
+ "syslog:x:104:108::/home/syslog:/bin/false\n"
+ "_apt:x:105:65534::/nonexistent:/bin/false\n"
+ "messagebus:x:106:110::/var/run/dbus:/bin/false\n"
+ "uuidd:x:107:111::/run/uuidd:/bin/false\n"
+ "student:x:1000:1000:student,,,:/home/student:/bin/bash\n"
+ "sshd:x:108:65534::/var/run/sshd:/usr/sbin/nologin\n\n"
+ "END:VCARD";
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