Java中的通配符匹配 [英] Wildcard matching in Java

查看:807
本文介绍了Java中的通配符匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的调试程序,它将输入简单的字符串作为输入,可以包含星号以指示通配符匹配 - 任何

I'm writing a simple debugging program that takes as input simple strings that can contain stars to indicate a wildcard match-any

*.wav  // matches <anything>.wav
(*, a) // matches (<anything>, a)

我以为我会简单地使用该模式,转义其中的任何正则表达式特殊字符,然后替换任何 \\ * 返回。* 。然后使用正则表达式匹配器。

I thought I would simply take that pattern, escape any regular expression special characters in it, then replace any \\* back to .*. And then use a regular expression matcher.

但我找不到任何Java函数来逃避正则表达式。我能找到的最佳匹配是 Pattern.quote ,但只需输入 \Q \ E 在字符串的开头和结尾。

But I can't find any Java function to escape a regular expression. The best match I could find is Pattern.quote, which however just puts \Q and \E at the begin and end of the string.

Java中是否有任何东西可以让你简单地进行通配符匹配而无需从头开始实现算法?

Is there anything in Java that allows you to simply do that wildcard matching without you having to implement the algorithm from scratch?

推荐答案

使用简单的正则表达式

这种方法的一个好处是我们可以轻松实现除 * 之外添加令牌(请参阅底部的添加令牌)。

One of this method's benefits is that we can easily add tokens besides * (see Adding Tokens at the bottom).

搜索: [^ *] + |(\ *)


  • 左侧 | 匹配任何不是明星的字符

  • 右侧将所有明星捕获到第1组

  • 如果第1组为空:替换为 \ Q +匹配+ E

  • 如果设置了第1组:替换为。*

  • The left side of the | matches any chars that are not a star
  • The right side captures all stars to Group 1
  • If Group 1 is empty: replace with \Q + Match + E
  • If Group 1 is set: replace with .*

这是一些工作代码(请参阅在线演示的输出)。

Here is some working code (see the output of the online demo).

输入:音频* 2012 * .wav

输出: \ Qaudio \ E. * \ Q2012 \ E. * \ Q.wav \ E

String subject = "audio*2012*.wav";
Pattern regex = Pattern.compile("[^*]+|(\\*)");
Matcher m = regex.matcher(subject);
StringBuffer b= new StringBuffer();
while (m.find()) {
    if(m.group(1) != null) m.appendReplacement(b, ".*");
    else m.appendReplacement(b, "\\\\Q" + m.group(0) + "\\\\E");
}
m.appendTail(b);
String replaced = b.toString();
System.out.println(replaced);

添加代币

假设我们还希望转换通配符,它代表单个字符,用点。我们只是在正则表达式中添加一个捕获组,并将其从左侧的matchall中排除:

Suppose we also want to convert the wildcard ?, which stands for a single character, by a dot. We just add a capture group to the regex, and exclude it from the matchall on the left:

搜索: [^ *?] + | (\ *)|(\?)

在替换函数中我们添加如下内容:

In the replace function we the add something like:

else if(m.group(2) != null) m.appendReplacement(b, "."); 

这篇关于Java中的通配符匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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