The following java code extracts the group, artifact and version using regex capture groups:
import java.util.regex.Pattern;
public class Main {
public static void main(String ... args) {
//Text to extract the group, artifact and version
var text = "org.junit.jupiter:junit-jupiter-api:5.7.0";
//Regex capture groups for Group:Artifact:Version
var pattern = "(.*):(.*):(.*)";
var compiledPattern = Pattern.compile(pattern);
var matcher = compiledPattern.matcher(text);
if (matcher.find( )) {
System.out.println("Whole text: " + matcher.group(0) );
System.out.println("Group: " + matcher.group(1) );
System.out.println("Artifact: " + matcher.group(2) );
System.out.println("Version: " + matcher.group(3) );
} else {
System.out.println("NO MATCH");
}
}
}
Output
Whole text: org.junit.jupiter:junit-jupiter-api:5.7.0
Group: org.junit.jupiter
Artifact: junit-jupiter-api
Version: 5.7.0
Leave a Reply