0

我有这些行:

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

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

c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e
4

5 回答 5

3

建议:使用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()
于 2013-06-13T16:22:03.073 回答
1

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

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
于 2013-06-13T16:22:29.493 回答
1

怎么样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
于 2013-06-13T16:23:46.327 回答
1

看起来像检索 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

ideone 上的演示

于 2013-06-13T16:25:52.463 回答
0

我认为你不能比匹配代表十六进制数字的 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);
    }
}

但我可能错了...

于 2013-06-13T16:29:12.647 回答