You can put your separators in there as literals to keep them out of captures:
$ cat /tmp/ver.pl #!perl while (<DATA>) { if ( /([\w+-]{3,})-([.\d-]+)\./ ) { print "$1 - $2\n"; } print "$_\n"; } __END__ 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 $ perl /tmp/ver.pl binutils - 2.23.52.0.1-12 binutils-2.23.52.0.1-12.el7.x86_64 compat-libcap1 - 1.10-3 compat-libcap1-1.10-3.el7.x86_64 compat-libstdc++-33 - 3.2.3-71 compat-libstdc++-33-3.2.3-71.el7.i686 But you may want to look at the options for rpm listing. There are many and they can specifically list the package version - you can create your own format for the listings. man rpm --qf|--queryformat QUERYFMT option, followed by the QUERYFMT format string. Query formats are modified versions of the standard printf(3) formatting. The format is made up of static strings (which may include standard C character escapes for newlines, tabs, and other special characters) and printf(3) type formatters. As rpm already knows the type to print, the type specifier must be omitted however, and replaced by the name of the header tag to be printed, enclosed by {} characters. Tag names are case insensitive, and the leading RPMTAG_ portion of the tag name may be omitted as well. On Thu, Aug 9, 2018 at 4:32 PM, Home Linux Info <homelinuxi...@gmail.com> wrote: > > Hello, > > You can begin with "*[a-zA-Z_+-]{3,}[0-9]*" to get the package name, it > needs a little more work for right now it gets the last dash and first > digit of package version. Then you can try "*([^a-zA-Z_+-]{3,})(.\d{1,})* > ". > The first regex gives the following result: > *binutils-2* > *compat-libcap1* > *compat-libstdc++-3* > Which is almost what you need while the second one is more exact as it > gives you: > *2.23.52.0.1-12* > *1.10-3* > *3.2.3-71* > And that looks like exactly what you need. > > I'm no expert in regex but I like to experiment with it to see if I can > extract some parts from a text / string using it. > > Jimmy (bash, perl and python total noob but trying to learn stuff). > > On 27.07.2018 15:54, Asad 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 > > > -- a Andy Bach, afb...@gmail.com 608 658-1890 cell 608 261-5738 wk