Say I want to make a regex that splits a optional version number from a file name i.e
val regex(name, ver) = "file.jar" // name = file, ver = empty
val regex(name, ver) = "some-software.jar" // name = some-software, ver = empty
val regex(name, ver) = "software-1.0.jar" // name = software, ver = 1.0
val regex(name, ver) = "some-file-1.0.jar" // name = some-file, ver = 1.0
How is such a regular expression written in Scala/Java ?. In perl I would do something along the lines of:
(.*)(-(\d|.)*)?.jar
but Scala does not seam to support making optional groups in this syntax.
I am not sure what your question now is.
I assume it is not matching the second group, because the first one is greedy and since the second is optional, the first matches everything.
Try this:
See it here on Regexr
.*?(?:. You will find the name now in group 1 and the version in group 2.