But if you have to use a regex, I suggest using the /x modifier to make it easier to read an maintain the regex:
#!/usr/bin/perl use strict; use warnings; for my $s (qw/binutils-2.23.52.0.1-12.el7.x86_64 compat-libcap1-1.10-3.el7.x86_64 compat-libstdc++-33-3.2.3-71.el7.i686/) { my ($name, $version, $build) = $s =~ m{ ^ (.*) # name - (.*) # version - ([0-9]+) # build [.] [^.]+ # os [.] [^.]+ \z # architecture }x; print "n $name v $version b $build\n"; } On Fri, Jul 27, 2018 at 9:14 AM Chas. Owens <chas.ow...@gmail.com> wrote: > I don't think a regex is the simplest and most maintainable way to get > this information. I think it is probably better to take advantage of the > structure of the string to discard and find information: > > #!/usr/bin/perl > > use strict; > use warnings; > > for my $s (qw/binutils-2.23.52.0.1-12.el7.x86_64 > compat-libcap1-1.10-3.el7.x86_64 compat-libstdc++-33-3.2.3-71.el7.i686/) { > my @dots = split /\,/, $s; > pop @dots; #get rid of architecture > pop @dots; #get rid of os > my $name_and_version = join "", @dots; > my @dashes = split /-/, $s; > my $build = pop @dashes; > my $version = pop @dashes; > my $name = join "-", @dashes; > print "n $name v $version b $build\n"; > } > > > > On Fri, Jul 27, 2018 at 8:57 AM Asad <asad.hasan2...@gmail.com> wrote: > >> Hi All , >> >> I want to get a regex to actually get the rpm name and version >> for comparison : >> >> >> binutils-2.23.52.0.1-12.el7.x86_64", >> compat-libcap1-1.10-3.el7.x86_64" >> compat-libstdc++-33-3.2.3-71.el7.i686 >> >> (^[a-zA-Z0-9\-]*)\-\d' >> >> First part of the regular expression is ^[a-zA-Z0-9\-] >> >> which means search for anything that begins with a letter >> >> (lower or upper) or a number up until you reach an >> >> hyphen sign (‘-‘). >> >> but it fails to match >> >> compat-libstdc++-33-3.2.3-71.el7.i686 >> >> Please let me know what regex should i use to extract all 3 >> >> rpms. >> >> Also let me know if there are web tools to build regex >> >> Good websites for regex tutorials. >> >> >> >> >> Thanks, >> >> >> >> >> >> -- >> Asad Hasan >> +91 9582111698 <+91%2095821%2011698> >> >