繁体   English   中英

将字符串与多个正则表达式模式匹配

[英]Match a string against multiple regex patterns

我有一个输入字符串。

我正在考虑如何有效地将此字符串与多个正则表达式匹配。

Example Input: ABCD

我想匹配这些reg-ex模式,如果至少其中一个匹配,则返回true

[a-zA-Z]{3}

^[^\\d].*

([\\w&&[^b]])*

我不确定如何一次匹配多个模式。 有人可以告诉我,我们如何有效地做到这一点?

如果你只有几个正则表达式,并且它们在编译时都是已知的,那么这就足够了:

private static final Pattern
  rx1 = Pattern.compile("..."),
  rx2 = Pattern.compile("..."),
  ...;

return rx1.matcher(s).matches() || rx2.matcher(s).matches() || ...;

如果它们中有更多,或者它们是在运行时加载的,那么使用模式列表:

final List<Pattern> rxs = new ArrayList<>();


for (Pattern rx : rxs) if (rx.matcher(input).matches()) return true;
return false;

你可以用一个大的正则表达式:

[a-zA-Z]{3}|^[^\\d].*|([\\w&&[^b]])*

我不确定effectively意味着什么,但如果它是关于性能而你想检查很多字符串,我会去寻找

...
static Pattern p1 = Pattern.compile("[a-zA-Z]{3}");
static Pattern p2 = Pattern.compile("^[^\\d].*");
static Pattern p3 = Pattern.compile("([\\w&&[^b]])*");

public static boolean test(String s){
   return p1.matcher(s).matches ? true: 
        p2.matcher(s).matches ? true: 
        p3.matcher(s).matches;
}

我不知道它将如何影响性能,但是他们都在一个正则表达式相结合| 也可以帮忙。

为避免重新创建Pattern和Matcher类的实例,您可以创建其中一个并重用它们。 要重用Matcher类,可以使用reset(newInput)方法。 警告 :此方法不是线程安全的。 仅当您可以保证只有一个线程能够使用此方法时才使用它,否则为每个方法调用创建单独的Matcher实例。

这是可能的代码示例之一

private static Matcher m1 = Pattern.compile("regex1").matcher("");
private static Matcher m2 = Pattern.compile("regex2").matcher("");
private static Matcher m3 = Pattern.compile("regex3").matcher("");

public boolean matchesAtLeastOneRegex(String input) {
    return     m1.reset(input).matches() 
            || m2.reset(input).matches()
            || m3.reset(input).matches();
}

这是另一种选择。 请注意,有一件事是不按特定顺序返回它们。 但是可以通过m.start()排序来做到这一点。

private static HashMap<String, String> regs = new HashMap<String, String>();

...

    regs.put("COMMA", ",");
    regs.put("ID", "[a-z][a-zA-Z0-9]*");
    regs.put("SEMI", ";");
    regs.put("GETS", ":=");
    regs.put("DOT", "\\.");

    for (HashMap.Entry<String, String> entry : regs.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        Matcher m = Pattern.compile(value).matcher("program var a, b, c; begin a := 0; end.");
        boolean f = m.find();
        while(f) 
        {
            System.out.println(key);
            System.out.print(m.group() + " ");
            System.out.print(m.start() + " ");
            System.out.println(m.end());
            f = m.find();
        }

    }   
}

就像在( 在String上运行多个正则表达式模式 )中解释的那样,最好将每个正则表达式连接到一个大的正则表达式,而不是只运行一个匹配器。 这是一个很大的改进,你经常重用正则表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM