G'day ghosh123,

"But the below code is not able to match when there is a '_' or '-' in the string just after "Users of". For example 'Fusion' is matching but not 'Galaxy-AdvTech' and 'Enterprise_VO' etc."

Rubbish! The code you posted isn't matching any of your sample data:

$ perl -Mstrict -Mwarnings -e ' while (<>) { if(/^Users of ([\w+\_\-]):\s+\(Total of ([0-9]+) licenses issu +ed;\s+Total of ([0-9]+) (licenses|license) in use/ ) { print "$1 |$2|$3 \n"; } else { print "No match!\n"; } } ' Users of Enterprise_VO: (Total of 1 license issued; Total of 0 license +s in use) No match! Users of Fusion: (Total of 4 licenses issued; Total of 0 licenses in u +se) No match! Users of Galaxy-AdvCTS: (Total of 5 licenses issued; Total of 0 licens +es in use) No match! Users of Galaxy-AdvTech: (Total of 5 licenses issued; Total of 0 licen +ses in use) No match! Users of Galaxy-Common: (Total of 30 licenses issued; Total of 1 licen +se in use) No match!

This is because firstly, [\w+\_\-] is only matching a single character which will either be: a word character (alphanumeric, underscore [and others - see perlrecharclass - Backslash sequences]), a plus sign or a hyphen (both backslashes are unnecessary and the underscore is already covered by the \w) - [\w-]+ is what you want here. And, secondly, the first instance of licenses does not match the word "license" - licenses? is what you want here. Putting all that together:

$ perl -Mstrict -Mwarnings -e ' while (<>) { if(/^Users of ([\w-]+):\s+\(Total of ([0-9]+) licenses? issued +;\s+Total of ([0-9]+) (licenses|license) in use/) { print "$1 |$2|$3 \n"; } else { print "No match!\n"; } } ' Users of Enterprise_VO: (Total of 1 license issued; Total of 0 license +s in use) Enterprise_VO |1|0 Users of Fusion: (Total of 4 licenses issued; Total of 0 licenses in u +se) Fusion |4|0 Users of Galaxy-AdvCTS: (Total of 5 licenses issued; Total of 0 licens +es in use) Galaxy-AdvCTS |5|0 Users of Galaxy-AdvTech: (Total of 5 licenses issued; Total of 0 licen +ses in use) Galaxy-AdvTech |5|0 Users of Galaxy-Common: (Total of 30 licenses issued; Total of 1 licen +se in use) Galaxy-Common |30|1

However, most of that regex is superfluous: /^Users of ([^:]+): \(Total of (\d+) [^0-9]+ (\d+)/ matches all of your supplied data:

$ perl -Mstrict -Mwarnings -E ' while (<>) { if (/^Users of ([^:]+): \(Total of (\d+) [^0-9]+ (\d+)/) { say "$1 $2 $3"; } } ' Users of Enterprise_VO: (Total of 1 license issued; Total of 0 license +s in use) Enterprise_VO 1 0 Users of Fusion: (Total of 4 licenses issued; Total of 0 licenses in u +se) Fusion 4 0 Users of Galaxy-AdvCTS: (Total of 5 licenses issued; Total of 0 licens +es in use) Galaxy-AdvCTS 5 0 Users of Galaxy-AdvTech: (Total of 5 licenses issued; Total of 0 licen +ses in use) Galaxy-AdvTech 5 0 Users of Galaxy-Common: (Total of 30 licenses issued; Total of 1 licen +se in use) Galaxy-Common 30 1

-- Ken


In reply to Re: regular expression not matching by kcott
in thread regular expression not matching by ghosh123

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.