Rewriting sbt artifacts with a regex matcher to strip version information
RegEx guru wanted: I want to strip library names in packaging an sbt project. Namely if my dependency is called
val in = "scala-swing-2.9.0-1.jar"
I want to re-write the name as
"scala-swing.jar"
E.g. given a method
def rewrite(p: util.matching.Regex, s: String) = s match {
case p(name) => name + ".jar"
case _ => s
}
I tried
rewrite("""([^-_]*)[-_].*.jar""".r, in) // gives "scala.jar"
and
rewrite("""(.*)[-_]\d.*.jar""".r, in) // gives "scala-swing-2.9.0.jar"
...
EDIT:
Bonu开发者_Python百科s questions:
val in2 = "audiowidgets_2.9.0-1-0.10-SNAPSHOT.jar"
require(rewrite( ???, in2 ) == "audiowidgets.jar")
val in3 = "jsyntaxpane-0.9.5-b29.jar"
require(rewrite( ???, in3 ) == "jsyntaxpane.jar")
Finally I know what they are talking about with the 'greedy' and 'reluctant' attributions...
val re = """(.*?)(?:[-_](?:(?:[^-_]*\d)|SNAPSHOT))+.jar""".r
rewrite( re, in ) // "scala-swing.jar"
rewrite( re, in2 ) // "audiowidgets.jar"
rewrite( re, in3 ) // "jsyntaxpane.jar"
You could perhaps get away with something simpler too:
"(.*?)[-_]\\d.*\\.jar".r
精彩评论