Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

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);
like image 421
user697911 Avatar asked Nov 06 '16 18:11

user697911


1 Answers

You can use this to combine patterns:

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

to match either one:

Matcher matcher = pattern.matcher(s);
like image 112
ItamarG3 Avatar answered Oct 02 '22 07:10

ItamarG3