I have these lines:
This reverts commit c289f6fa1f8642a5caf728ef8ff87afd5718cd99.
This reverts commit c7740a943ec896247ebc5514b6be02710caf3c53. There should
This reverts 518920b764ee9150781e68217181b24d0712748e commit.
How can i do with regular expression on java
to retrieve only numbers:
c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e
Recommendation: use JGit .
If you really insist on doing this using a regular expression, then you can use this regex:
\b[a-f0-9]{40}\b
using:
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()
If you need the full alphanumeric hashes rather than only digits, consider using this example:
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));
}
Output:
c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e
How about This reverts (?:commit )?([af\\\\d]+)
? This should store searched part in group 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));
output:
c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e
It looks like a little hack of retrieving the sequence of 40 alphanumeric characters should do the trick. Use this pattern \\p{Alnum}{40}
; the only match in your test string is going to be the commit number.
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());
}
}
}
This prints
c289f6fa1f8642a5caf728ef8ff87afd5718cd99
c7740a943ec896247ebc5514b6be02710caf3c53
518920b764ee9150781e68217181b24d0712748e
I don't think you can do a better job than matching sequences of 40 character representing hexadecimal numbers.
Here is a full example (could be refined but it's the idea):
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);
}
}
But I may be wrong...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.