【问题标题】:Retrieve a String using regular expression on java在java上使用正则表达式检索字符串
【发布时间】:2013-06-13 16:14:52
【问题描述】:

我有以下几行:

This reverts commit c289f6fa1f8642a5caf728ef8ff87afd5718cd99.
This reverts commit c7740a943ec896247ebc5514b6be02710caf3c53.  There should
This reverts 518920b764ee9150781e68217181b24d0712748e commit.

如何使用正则表达式 on java 来仅检索数字:

c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e

【问题讨论】:

  • 所以你想摆脱所有的单词和.?你可以使用String#split("\\s+")
  • 如果你想在 Java 中操作 git 存储库,there is a library for that
  • c 不是数字。你的意思是十六进制。
  • 你确定在这种情况下你只会有这些哈希吗?如果您打算管理所有搜索“reverts”的 GIT 消息是不够的......

标签: java regex expression


【解决方案1】:

建议:使用JGit

如果你真的坚持使用正则表达式,那么你可以使用这个正则表达式:

\b[a-f0-9]{40}\b

使用:

final Pattern sha1Pattern = Pattern.compile("\\b[a-f0-9]{40}\\b");

final Matcher matcher = sha1Pattern.matcher(yourInput);
if (matcher.find())
    // sha1 is accessed via matcher.group()

【讨论】:

  • 我不知道任何仅包含 a-f 字母的 40 个字母的单词,这可能会产生误报结果,所以 +1 :) 顺便说一句,您可能想将 20 更改为 40
  • @Pshemo 哎呀...我似乎总是忘记每个字节需要 两个 十六进制数字:p
  • 它不起作用!可能你的正则表达式出错了?
  • @Mehdi 没有理由为什么它不应该工作,但同样,如果我要在 Java 中操作 git 存储库,我会采用 JGit 方式;)
  • @Mehdi 是的,20 是原因...\\b 是单词锚,[a-f0-9] 是字符类。 {n} 是一个量词,意思是“正好是n 次”
【解决方案2】:

如果您需要完整的字母数字哈希而不是数字,请考虑使用以下示例:

String test1 = "This reverts commit c289f6fa1f8642a5caf728ef8ff87afd5718cd99.";
String test2 = "This reverts commit c7740a943ec896247ebc5514b6be02710caf3c53.  There should";
String test3 = "This reverts 518920b764ee9150781e68217181b24d0712748e commit.";
Pattern pattern = Pattern.compile("reverts\\s(commit\\s)*(.+?)[\\.\\s]");
Matcher matcher = pattern.matcher(test1);
if (matcher.find()) {
    System.out.println(matcher.group(2));
}
matcher = pattern.matcher(test2);
if (matcher.find()) {
    System.out.println(matcher.group(2));
}
matcher = pattern.matcher(test3);
if (matcher.find()) {
    System.out.println(matcher.group(2));
}

输出:

c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e

【讨论】:

    【解决方案3】:

    它看起来像检索 40 个字母数字字符序列的小技巧应该可以解决问题。使用此模式\p{Alnum}{40};测试字符串中唯一的匹配项将是提交号。

    static final String[] data = new String[] {
        "This reverts commit c289f6fa1f8642a5caf728ef8ff87afd5718cd99.",
        "This reverts commit c7740a943ec896247ebc5514b6be02710caf3c53.  There should",
        "This reverts 518920b764ee9150781e68217181b24d0712748e commit."
    };
    public static void main (String[] args) throws java.lang.Exception {
        Pattern p = Pattern.compile("\\p{Alnum}{40}");
        for (String s : data) {
            Matcher m = p.matcher(s);
            if (m.find()) {
                 System.out.println(m.group());   
            }
        }
    }
    

    打印出来

    c289f6fa1f8642a5caf728ef8ff87afd5718cd99
    c7740a943ec896247ebc5514b6be02710caf3c53
    518920b764ee9150781e68217181b24d0712748e
    

    Demo on ideone.

    【讨论】:

    • 我认为哈希仅由十六进制数字组成,因此您可以更具限制性:[a-f0-9]
    【解决方案4】:

    This reverts (?:commit )?([a-f\\d]+) 怎么样?这应该将搜索到的部分存储在第 1 组中

    String data="This reverts commit c289f6fa1f8642a5caf728ef8ff87afd5718cd99." +
            "This reverts commit c7740a943ec896247ebc5514b6be02710caf3c53.  There should" +
            "This reverts 518920b764ee9150781e68217181b24d0712748e commit.";
    
    Matcher m = Pattern.compile("This reverts (?:commit )?([a-f\\d]+)").matcher(data);
    while(m.find())
        System.out.println(m.group(1));
    

    输出:

    c289f6fa1f8642a5caf728ef8ff87afd5718cd99
    c7740a943ec896247ebc5514b6be02710caf3c53
    518920b764ee9150781e68217181b24d0712748e
    

    【讨论】:

    • 恕我直言,不够通用,这种散列可能与其他消息一起出现,而不是 OP 提供的说明这个想法的消息,但是这个子集很好。
    • @Pragmateek 是的,但在不知道这个问题的真正目的的情况下,很难给出更一般的答案。 fge 的答案可能是关闭,但不确定 OP 输入是否不会包含 caaaaaaafeeeeeeeeeeeee(repeat e until 40 letter word) 之类的字符串 :)
    • 正确。而且这么长的词真的很糟糕,即使在法语中我们也没有;)但如果是为了管理一些与化学工业相关的代码:worldwidewords.org/qa/qa-imm1.htm 应该是可能的,如果没有“i”
    • @Pragmateek 好一个:D。我还想知道 OP 输入是否不会包含任何仅包含 a-f 字符的化学公式,例如 Ac B Ba Be C Ca Cd Ce Cf Db F Fe 和数字 :)
    【解决方案5】:

    我认为没有比匹配代表十六进制数字的 40 个字符的序列更好的工作了。

    这是一个完整的例子(可以改进,但它的想法):

    public static void main(String[] args) throws Exception
    {   
        String s = "This reverts commit c289f6fa1f8642a5caf728ef8ff87afd5718cd99.\n" + 
                   "This reverts commit c7740a943ec896247ebc5514b6be02710caf3c53.  There should\n"+
                   "This reverts 518920b764ee9150781e68217181b24d0712748e commit.\n";
    
        Pattern pattern = Pattern.compile("[a-f0-9]{40}");
        Matcher matcher = pattern.matcher(s);
    
        while (matcher.find())
        {
            String m = matcher.group();
    
            System.out.println(m);
        }
    }
    

    但我可能错了……

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多