使用java上的正则表达式检索String

时间:2013-06-13 16:14:52

标签: java regex expression

我有以下几行:

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

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

c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e

5 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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

Demo on ideone

答案 4 :(得分: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);
    }
}

但我可能错了......