Reputation: 367
I have rpm
packages that are named like pkg-name.0.15.0-6.x86_64.rpm
. I want to extract two strings out of this:
pkg-name
pkg-name.0.15.0-6
I tried this code below but I am not getting any match and I cannot figure out why
fullRpmName = "pkg-name.0.15.0-6.x86_64.rpm"
def matcher = (fullRpmName =~ /((.*)\\.\\d+\\.\\d+.\\d+-\\d+\\..*).x86_64\\.rpm/)
println matcher.group(1)
println matcher.group(2)
Upvotes: 0
Views: 302
Reputation: 626845
You can use
String fullRpmName = "pkg-name.0.15.0-6.x86_64.rpm"
def matcher = fullRpmName =~ /^((.*?)\..*)\.x86_64\.rpm/
if (matcher) {
println matcher.group(2) // pkg-name
println matcher.group(1) // pkg-name.0.15.0-6
}
See the Groovy demo online. See the regex demo.
Details
^
- start of string((.*?)\..*)
- Group 1:
(.*?)
- Group 2: any zero or more chars other than line break chars as few as possible\.
- a dot.*
- any zero or more chars other than line break chars as many as possible\.x86_64\.rpm
- a .x86_64.rpm
string.A bit more complex is a regex like ^(([^.]*)(?:[-.]\d+)*)\.x86_64\.rpm
(see demo):
[^.]*
- zero or more chars other than a dot(?:[-.]\d+)*
- zero or more repetitions of a -
or .
followed with one or more digits.Upvotes: 0
Reputation: 2213
I modified your regex to match the package naming convention, you can see a working example here:
((.*)\.\d+\.\d+\.\d+-\d+)\.x86_64\.rpm
Upvotes: 1