是否可以基于Java中的两个子模式构建模式 [英] Is it possible to build a Pattern based on two sub patterns in Java

查看:58
本文介绍了是否可以基于Java中的两个子模式构建模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

由于p1和p2都相当长,因此很难编写出单个模式来覆盖p1和p2中的所有情况.是否可以编写基于p1和p2的另一个模式p3,这样我只能运行一个Matcher:

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);

推荐答案

您可以使用它来组合模式:

You can use this to combine patterns:

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

匹配任意一个:

Matcher matcher = pattern.matcher(s);

这篇关于是否可以基于Java中的两个子模式构建模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆