For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > January 2006 > Printing The Match Pattern









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Printing The Match Pattern
VijayPothireddy

2006-01-03, 11:59 pm

I have a file with entries like

C:\temp\stage\Components
C:\temp\stage\Components\yt.swdt.outi
C:\temp\stage\Components\yt.swdt.outi\3.2.5.0
C:\temp\stage\Components\yt.swdt.outi\3.2.5.0\1
C:\temp\stage\Components\yt.swdt.outi\3.2.5.0\1\fjh

C:\temp\stage\Components
C:\temp\stage\Components\jj.tyd.fre
C:\temp\stage\Components\jj.tyd.fre\3.2.5.0
C:\temp\stage\Components\jj.tyd.fre\3.2.5.0\1
C:\temp\stage\Components\jj.tyd.fre\3.2.5.0\1\fjh


I want to extract text with a pattern like “Components\yt.swdt.outi\3.2.5.0” from all the lines. so I wrote the following regular expression


if($temp =~ m/Components\\.*\\/ig)
{
print "\nMatching : $1";
}

When I execute the program I am getting blank output.
I am new baby to perl. So I am not able to trouble shoot. Your help will be very much appreciated.
displeaser

2006-01-05, 6:47 am

Hi,

the problem lies with the fact you are not capturing anything into $1.

Your code states:

if($temp =~ m/Components\\.*\\/ig)
{
print "\nMatching : $1";
}

You will need to use capturing parenthesis as follows.

if($temp =~ m/(Components\\.*\\)/ig)
{
print "\nMatching : $1";
}

Note the ( and ) around what you are tring to match.

If you use multiple () then the values get captured to $1, $2, $3 depending on the number of brackets in your match.

You might find this link helpful:
http://perldoc.perl.org/perlrequick...racting-matches

also you should use the following in all but the most trivial scripts (and sometimes even then)
use strict;
use warnings;

This will pull up many errors, bugs and misuses of Perl.

Hope this helps you.

Displeaser.

ps. Welcome to Perl :-)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com