[Ilugc] Perl regular expression

  • From: solar345@xxxxxxxxx (Antano Solar)
  • Date: Tue Sep 25 21:19:37 2007


 I want to grep from the  file, the words which start with  & and close
with  ; eg(   ,& ...) to write this is in to another file.  how
could i  fetch an individual word. from the file.  It is regx is an ideal
way for using this



you could do something like this assuming you give the input  filename(s) as 
arguments and output filename is resultfile .


open(RESULTFILE ,">resultfile") || die("Cannot Open File");
foreach(<>)
{

foreach  (split(/ /,$_))
{
         print RESULTFILE  $1."\n" if(m/^&(\w+)$\;/)  
}

  
}

This is just a 2 min script to give you an idea.
Remember a line has been split into words so tat if there is more than 1 word 
in a line all of them get matched. ofcourse  you can try the m//g approach 
and avoid one inner loop .
You can optimize this further and yet make it a one liner 


 please help me with the examples or the resources where i could dig more
abt
 regx.


You can learn Perl re from 
http://perldoc.perl.org/perlre.html

With Regards

Antano Solar John

Other related posts: