user697911 user697911 - 1 year ago 73
Java Question

Is it possible to build a Pattern based on two sub patterns in Java

Pattern p1 = Pattern.compile(".................");
Pattern p2 = Pattern.compile("xxxxxxxxxxxxxxxxxxx");

Since both p1 and p2 are quite long, and it's hard to write a single pattern to cover all cases in p1 and p2. Is it possible to write another pattern p3 that is built upon on p1 and p2, so that I can only run one Matcher:

Matcher m = p3.matcher(str);

Answer Source

You can use this to combine patterns:

Pattern pattern = Pattern.compile(".................|xxxxxxxxxxxxxxxxxxx");

to match either one:

Matcher matcher = pattern.matcher(s);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download