[clug-talk] Perl regular expressions ?

Michael Gale michael.gale at bluesuperman.com
Mon Mar 21 19:09:35 PST 2005


AH HA ...

I got it figured out :)

Here is the code, I will comment it so other people can know:
#!/usr/bin/perl -w

while(<STDIN>)
{
my($input) = $_; #Read strings in on the command line
chomp($input);   #Removes trailing line feed

if ($input =~ "DONE")  #If string is contains DONE exit
         {
                 exit;
         }

print "#################\n";
print "### OUTPUT IS ###\n";
print "#################\n";
print "### ";
print $input =~ /(\d+);(\d+);(\d+);(\d+)/;
## Look for a pattern that matchs number;number;number;number
## \d means match a number
## + means any amount of numbers
## /()/ assigns the match to $1,$2 and so on
print "\n";
print "\$1 = $1\n";
print "\$2 = $2\n";
print "\$3 = $3\n";
print "\$4 = $4\n";
print "\n";
print "#################\n";
print "\n\n\n";

}

So when I run the script is passed it "Memory Usage: 1234;678;000;4567" 
which is sort of like the out put from a nagios memory check on windows 
boxes ;)

Now if you simple print:
print $input it will equal "Memory Usage: 1234;678;000;4567"
print $input =~ /(\d+);(\d+);(\d+);(\d+)/; it will produce "12346780004567"

which may be confusing ... because what happens to $1 ? Remember because 
"/(pattern)/" is supposed to assign the match to a $1 and $2 and so on. 
It actually does ... it just gets printed with out any spaces.

If you print $1,$2,$3,$4 you will see how they are divided:
--snip--
[michael at roadwarrior Desktop]$ ./rgperl
Memory Usage: 1234;678;000;4567
#################
### OUTPUT IS ###
#################
### 12346780004567
$1 = 1234
$2 = 678
$3 = 000
$4 = 4567

#################
--snip--

Hope this helps other people figure out where the arguments can come from :)

Michael.



Michael Gale wrote:
> Hello,
> 
> I wrote this little test script to my regular expressions:
> 
> #!/usr/bin/perl -w
> 
> while(<STDIN>)
> {
> my($input) = $_;
> chomp($input);
> 
> #my($input,$check) = $input =~ m/(.*\|)(.*)$/;
> 
> if ($input =~ "DONE")
>         {
>                 exit;
>         }
> 
> $input =~ /.*(\d+)/;
> 
> 
> print "#################\n";
> print "### OUTPUT IS ###\n";
> print "#################\n";
> print "### ";
> print $_;
> print "\n";
> print "#################\n";
> print "\n\n\n";
> 
> }
> 
> 
> Michael.
> 
> 
> Michael Gale wrote:
> 
>> Hello,
>>
>> I am going to start playing around with perl regular expressions but 
>> first have some questions I am hoping someone on the list can answer ?
>>
>> 1. Can I test them from the command line ?
>>
>> echo "some string of text" | perl place_regular_expression_here
>>
>> 2. How would I go about producing arguments are out put, for example:
>>
>> echo "Memory Usage: 25% used warning 75%" | perl 'Memory.Usage:.\d+'
>>
>> The about would produce "Memory Usage: 25" but I only want to output 
>> the 25 but I also need to make sure that the first part contains 
>> "Memory Usage:" ?
>>
>> Thanks.
>>
>> Michael.
>>
>> _______________________________________________
>> clug-talk mailing list
>> clug-talk at clug.ca
>> http://clug.ca/mailman/listinfo/clug-talk_clug.ca
>> Mailing List Guidelines (http://clug.ca/ml_guidelines.php)
>> **Please remove these lines when replying
> 
> 
> _______________________________________________
> clug-talk mailing list
> clug-talk at clug.ca
> http://clug.ca/mailman/listinfo/clug-talk_clug.ca
> Mailing List Guidelines (http://clug.ca/ml_guidelines.php)
> **Please remove these lines when replying



More information about the clug-talk mailing list