Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Regexp query

Question posted by: poolboi (Familiar Sight) on May 12th, 2008 08:28 AM
hey guys,

this is one regexp i did and it worked
but i dunno if it's correct
could anyone verfy

for the sentence in the file

/* 1 TB Hi VTP-10 SESSION=01471 USERID=##$@@# STARTED 2008-05-09 09:17:00 */

i want to match things between 1 TBHLR to USERID

so i used this
Code: ( text )
  1. if ($data1 =~ /1 TBHLR.+USERID/)


is this matching alright?
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
nithinpes's Avatar
nithinpes
Expert
224 Posts
May 12th, 2008
09:10 AM
#2

Re: Regexp query
The pattern you have used will match lines containing '1 TBHLR' followed by one/ more characters and then followed by 'USERID'. But if you want to extract characters between these two expressions, you have to group those characters and assign it to a variable as follows:
Code: ( text )
  1. open(FILE,"data.txt") or die "opening file failed";
  2. while(<FILE>)  {
  3. if(/1 TBHLR(.+)USERID/)  {  ## group characters i between using parantheses
  4.   my $content=$1; 
  5. # $1 is the special variable carrying pattern matched inside
  6. # parantheses number 1. 
  7.   print "$content\n";   ### you can even print $1 directly
  8. }
  9. }


There is no 'TBHLR' in the sample line that you provided :)

Reply
poolboi's Avatar
poolboi
Familiar Sight
168 Posts
May 13th, 2008
03:28 AM
#3

Re: Regexp query
thanks!

yup.. i change my code forgot to change the TBHLR part
so since this syntaxt matches then i guess it's alright
just wanna get opinions if the syntax correct

Reply
Reply
Not the answer you were looking for? Post your question . . .
174,847 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Perl Forum Contributors