in reply to regular expression not matching
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
|
|---|