Home > Net >  Groovy regex not matching
Groovy regex not matching

Time:08-12

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:

  1. Just the name without version, release, or architecture. So in this case it would be pkg-name
  2. Then I want to extract the name with the version and release. So in this case it would be 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)

CodePudding user response:

I modified your regex to match the package naming convention, you can see a working example here:

((.*)\.\d \.\d \.\d -\d )\.x86_64\.rpm

CodePudding user response:

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.
  • Related