首页 \ 问答 \ “glob”类型模式是否有相当于java.util.regex的等价物?(Is there an equivalent of java.util.regex for “glob” type patterns?)

“glob”类型模式是否有相当于java.util.regex的等价物?(Is there an equivalent of java.util.regex for “glob” type patterns?)

有没有一个标准(最好是Apache Commons或类似的非病毒)库,用于在Java中进行“glob”类型的匹配? 当我不得不在Perl中做类似的时候,我把所有的“ . ”改为“ \. ”,“ * ”到“ .* ”和“ ? ”到“”这样的事情,但是我我想知道有人为我做了工作吗?

类似的问题: 从glob表达式创建正则表达式


Is there a standard (preferably Apache Commons or similarly non-viral) library for doing "glob" type matches in Java? When I had to do similar in Perl once, I just changed all the "." to "\.", the "*" to ".*" and the "?" to "." and that sort of thing, but I'm wondering if somebody has done the work for me.

Similar question: Create regex from glob expression


原文:https://stackoverflow.com/questions/1247772
更新时间:2021-06-05 19:06

最满意答案

没有什么内置的,但是将glob类似的转换为正则表达式很简单:

public static String createRegexFromGlob(String glob)
{
    String out = "^";
    for(int i = 0; i < glob.length(); ++i)
    {
        final char c = glob.charAt(i);
        switch(c)
        {
        case '*': out += ".*"; break;
        case '?': out += '.'; break;
        case '.': out += "\\."; break;
        case '\\': out += "\\\\"; break;
        default: out += c;
        }
    }
    out += '$';
    return out;
}

这对我来说很有用,但是我不知道它是否涵盖了“标准”,如果有的话

Paul Tomblin的更新:我发现一个执行glob转换的Perl程序,并将其适用于Java我最终得到:

    private String convertGlobToRegEx(String line)
    {
    LOG.info("got line [" + line + "]");
    line = line.trim();
    int strLen = line.length();
    StringBuilder sb = new StringBuilder(strLen);
    // Remove beginning and ending * globs because they're useless
    if (line.startsWith("*"))
    {
        line = line.substring(1);
        strLen--;
    }
    if (line.endsWith("*"))
    {
        line = line.substring(0, strLen-1);
        strLen--;
    }
    boolean escaping = false;
    int inCurlies = 0;
    for (char currentChar : line.toCharArray())
    {
        switch (currentChar)
        {
        case '*':
            if (escaping)
                sb.append("\\*");
            else
                sb.append(".*");
            escaping = false;
            break;
        case '?':
            if (escaping)
                sb.append("\\?");
            else
                sb.append('.');
            escaping = false;
            break;
        case '.':
        case '(':
        case ')':
        case '+':
        case '|':
        case '^':
        case '$':
        case '@':
        case '%':
            sb.append('\\');
            sb.append(currentChar);
            escaping = false;
            break;
        case '\\':
            if (escaping)
            {
                sb.append("\\\\");
                escaping = false;
            }
            else
                escaping = true;
            break;
        case '{':
            if (escaping)
            {
                sb.append("\\{");
            }
            else
            {
                sb.append('(');
                inCurlies++;
            }
            escaping = false;
            break;
        case '}':
            if (inCurlies > 0 && !escaping)
            {
                sb.append(')');
                inCurlies--;
            }
            else if (escaping)
                sb.append("\\}");
            else
                sb.append("}");
            escaping = false;
            break;
        case ',':
            if (inCurlies > 0 && !escaping)
            {
                sb.append('|');
            }
            else if (escaping)
                sb.append("\\,");
            else
                sb.append(",");
            break;
        default:
            escaping = false;
            sb.append(currentChar);
        }
    }
    return sb.toString();
}

我正在编辑这个答案,而不是自己做,因为这个答案让我在正确的轨道上。


There's nothing built-in, but it's pretty simple to convert something glob-like to a regex:

public static String createRegexFromGlob(String glob)
{
    String out = "^";
    for(int i = 0; i < glob.length(); ++i)
    {
        final char c = glob.charAt(i);
        switch(c)
        {
        case '*': out += ".*"; break;
        case '?': out += '.'; break;
        case '.': out += "\\."; break;
        case '\\': out += "\\\\"; break;
        default: out += c;
        }
    }
    out += '$';
    return out;
}

this works for me, but I'm not sure if it covers the glob "standard", if there is one :)

Update by Paul Tomblin: I found a perl program that does glob conversion, and adapting it to Java I end up with:

    private String convertGlobToRegEx(String line)
    {
    LOG.info("got line [" + line + "]");
    line = line.trim();
    int strLen = line.length();
    StringBuilder sb = new StringBuilder(strLen);
    // Remove beginning and ending * globs because they're useless
    if (line.startsWith("*"))
    {
        line = line.substring(1);
        strLen--;
    }
    if (line.endsWith("*"))
    {
        line = line.substring(0, strLen-1);
        strLen--;
    }
    boolean escaping = false;
    int inCurlies = 0;
    for (char currentChar : line.toCharArray())
    {
        switch (currentChar)
        {
        case '*':
            if (escaping)
                sb.append("\\*");
            else
                sb.append(".*");
            escaping = false;
            break;
        case '?':
            if (escaping)
                sb.append("\\?");
            else
                sb.append('.');
            escaping = false;
            break;
        case '.':
        case '(':
        case ')':
        case '+':
        case '|':
        case '^':
        case '$':
        case '@':
        case '%':
            sb.append('\\');
            sb.append(currentChar);
            escaping = false;
            break;
        case '\\':
            if (escaping)
            {
                sb.append("\\\\");
                escaping = false;
            }
            else
                escaping = true;
            break;
        case '{':
            if (escaping)
            {
                sb.append("\\{");
            }
            else
            {
                sb.append('(');
                inCurlies++;
            }
            escaping = false;
            break;
        case '}':
            if (inCurlies > 0 && !escaping)
            {
                sb.append(')');
                inCurlies--;
            }
            else if (escaping)
                sb.append("\\}");
            else
                sb.append("}");
            escaping = false;
            break;
        case ',':
            if (inCurlies > 0 && !escaping)
            {
                sb.append('|');
            }
            else if (escaping)
                sb.append("\\,");
            else
                sb.append(",");
            break;
        default:
            escaping = false;
            sb.append(currentChar);
        }
    }
    return sb.toString();
}

I'm editing into this answer rather than making my own because this answer put me on the right track.

相关问答

更多

什么是java.util.regex的C#等价物?(What is the C# equivalent of java.util.regex?)

System.Text.RegularExpressions.Regex类是.NET Framework的等价物。 我链接到的MSDN页面包含一个简单的示例。 I created the C# equivalent of the Java code in the question as: string myString = "B12"; Regex rx = new Regex(@"[A-Za-z](\\d+)"); MatchCollection matches = rx.Matches(myS ...

“glob”类型模式是否有相当于java.util.regex的等价物?(Is there an equivalent of java.util.regex for “glob” type patterns?)

没有什么内置的,但是将glob类似的转换为正则表达式很简单: public static String createRegexFromGlob(String glob) { String out = "^"; for(int i = 0; i < glob.length(); ++i) { final char c = glob.charAt(i); switch(c) { case '*': out += " ...

java.util.regex中(java.util.regex)

包含单个P字符的字符串是: 一个(可能为空)包含非P字符的字符串 一个P 一个(可能为空)包含非P字符的字符串 在正则表达式格式中,这是: ^[^P]*P[^P]*$ 哪里: ^匹配字符串的开头 [^P]*匹配除P以外的任何字符,0次或更多次 P匹配P [^P]*匹配除P以外的任何字符,0次或更多次 $匹配字符串的结尾 [^P*]和[^P]*的区别是: [^P*]匹配任何既不是P也不是*的单个字符 [^P]*匹配零个或多个不是P的字符 *的位置在这里当然很重要。 在[]内部, *没有任何特殊含义 ...

Glob正则表达式模式 - PHP(Glob regex patterns - PHP)

glob()不采用正则表达式,它采用简单的模式,只用任何东西替换* jokers,并且? 一个字符。 您可以尝试使用以下内容: $files = array_filter(glob('*.{jpg,png,gif}', GLOB_BRACE), function($file) { return preg_match('/^Images_(\d+)_([a-zA-Z]+)_([A-Z]+)\.(jpg|png|gif)$/', basename($file)); }); 这在内存方面并不是很有 ...

使用java.util.regex的HTML正则表达式(Regex for HTML with java.util.regex)

使用像Jsoup这样的HTML解析器。 String html1 = "<div xmlns=\"http://www.w3.org/1999/xhtml\"> <p/> <p/><p/> <p/></div>"; String html2 = "<div xmlns=\"http://www.w3.org/1999/xhtml\"> text<p/> <p/><p/>text <p/> </div>"; System.out.println(Js ...

将glob转换为正则表达式或使用Perl处理glob模式(Either convert a glob to regex or have Perl handle glob patterns)

鉴于: ? 只匹配一个字符,除了'/' *匹配除“/”之外的零个或多个字符 **匹配任何包括/ 如果你不关心格式检查和一些像'***'这样的极端情况,那么下面的策略,你首先将特殊字符转换为自定义设计的转义序列,然后将转义序列转换为最终字符串,可能会工作: my $rgx="^$glob\$"; $rgx=~ s|!|!e|g; $rgx=~ s|[+]|!p|g; $rgx=~ s|[*]{2}|!d|g; $rgx=~ s|[*]|!s|g; $rgx=~ s|[?]|!q|g; $rgx=~ ...

导入java.util.regex失败(import java.util.regex fails)

您的导入定义错误。 您将需要提供每个类的显式导入,如下所示: import java.util.regex.Matcher; import java.util.regex.Pattern; 或者做 import java.util.regex.*; 你正试图导入一个包,你需要*元字符。 如果你阅读编译器给你的消息,它说它找不到类正则表达式。 Your import is defined wrong. You'll either need to provide explicit imports ...

在JavaScript中,是否有像glob这样的URL?(Is there something like glob but for URLs, in JavaScript?)

你可以从像这样的函数开始,用于类似行为的行为: function glob(pattern, input) { var re = new RegExp(pattern.replace(/([.?+^$[\]\\(){}|\/-])/g, "\\$1").replace(/\*/g, '.*')); return re.test(input); } 然后将其称为: glob('http://*.example.org/*', 'http://foo.example.org/bar/ ...

Jakarta Regexp和Java 6 java.util.regex之间的区别(Differences between Jakarta Regexp and Java 6 java.util.regex)

java.util.regex.Matcher.matches()方法将尝试将完整的输入字符串与正则表达式匹配,这将是false 。 如果要在输入字符串中搜索模式,则需要使用java.util.regex.Matcher.find()方法: result = matcher.find(); // returns true The java.util.regex.Matcher.matches() method will try to match the complete input strin ...

如何在php glob()中定义多个模式(How to define multiple patterns in php glob())

您可以使用GLOB_BRACE常量 GLOB_BRACE - 扩展{a,b,c}以匹配'a','b'或'c' 例如 $dirname = 'uploads/'; glob("$dirname*.{png,jpeg,jpg,gif}", GLOB_BRACE); 见: http : //php.net/manual/en/function.glob.php You can use the GLOB_BRACE constant GLOB_BRACE - Expands {a,b,c} to mat ...

相关文章

更多

最新问答

更多
  • 如何从远程文件拉取文件而不覆盖本地文件?(How do I pull files from remote without overwriting local files?)
  • Reactjs:状态改变时重新渲染iframe(Reactjs: re-renders iframes when state changed)
  • 奇怪的网址,以及跟随php页面流程的困难(odd url, and difficulty in following the php page flow)
  • 标签活动无效(Tab Activity is not working)
  • JavaME合适的语法编译器建议?(JavaME-suitable grammar compiler recommendations?)
  • 指定参数(Specifying arguments)
  • 可以通过Ruby插件或控制台覆盖Sketchup中的键盘快捷键吗?(Can one override keyboard shortcuts in Sketchup through the a Ruby Plugin or Console?)
  • 计算Java EE Web App中用户数的最佳方法(Best way to count number of users in a Java EE web App)
  • 无法使用templateUrl加载cordova中的外部模板(unable to load external templates in cordova with templateUrl)
  • PHPExcel:写入期间无法使用缓存(PHPExcel: Unable to use cache during write)
  • 在javascript中嵌套这个指针(nested this pointer in javascript)
  • 谁跟领航致远培训过,有问题问下啊
  • 控制器要求在入门时下载(Controller ask to download on entry)
  • 未能通过conda安装Asyncio(Failure to install Asyncio via conda)
  • 如何查找已完成项目的总长度?(How to find length of total completed items?)
  • 如何检查OleInitialize是否已被调用?(How to check if OleInitialize has already been called?)
  • SQL在特定范围内返回列中具有最大值的行(SQL Returning rows with max value in column, within a specific range)
  • preg_match从url获取id(preg_match get the id from url)
  • 如何在运算符中为make方程转换perl变量?(How to convert a perl variable in a operator for make equations?)
  • 在导航上方添加空格/标题。(Add a white space/ header above navigation.)
  • MeetingItem已保存;(MeetingItem saved; but change now shown in Calendar)
  • c#vb:我们应该使用System.Lazy进行资源密集型任务吗?(c# vb: Should we use System.Lazy for resource-intensive task? (when threading is not needed))
  • 为什么在armeabi代码中使用armeabi-v7a代码?(Why use armeabi-v7a code over armeabi code?)
  • 获取请求的自定义标头(Java HTTP)(Fetching a custom header of a request (Java HTTP))
  • 是否可以在嵌套的if语句中从varchar转换为numeric以动态评估参数?(Is it possible to convert from varchar to numeric within a nested if statement in order to dynamically evaluate a parameter?)
  • 如何将Html.ActionLink转换为链接到Ajax调用的按钮?(How to convert from Html.ActionLink to a button linked to Ajax call?)
  • 应用程序如何处理Windows符号链接?(How are Windows symbolic links treated by the apps?)
  • html,js,css在jsfiddle中工作,但不在sharepoint中(html, js, css works in jsfiddle but not in sharepoint)
  • 从Ruby脚本调用Elasticsearch Rest API(Calling Elasticsearch Rest API from Ruby script)
  • 如何将嵌套setTimeouts转换为承诺(How to convert nested setTimeouts to promises)