By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,278 Members | 1,566 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,278 IT Pros & Developers. It's quick & easy.

Regexp query

P: 170
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
Expand|Select|Wrap|Line Numbers
  1. if ($data1 =~ /1 TBHLR.+USERID/)
is this matching alright?
May 12 '08 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 410
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:
Expand|Select|Wrap|Line Numbers
  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 :)
May 12 '08 #2

P: 170

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
May 13 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.