Regexp query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poolboi
    New Member
    • Jan 2008
    • 170

    Regexp query

    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=perl]
    if ($data1 =~ /1 TBHLR.+USERID/)
    [/CODE]

    is this matching alright?
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    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:
    open(FILE,"data.txt") or die "opening file failed";
    while(<FILE>)  {
    if(/1 TBHLR(.+)USERID/)  {  ## group characters i between using parantheses
      my $content=$1;  
    # $1 is the special variable carrying pattern matched inside
    # parantheses number 1.  
      print "$content\n";   ### you can even print $1 directly
    }
    }
    There is no 'TBHLR' in the sample line that you provided :)

    Comment

    • poolboi
      New Member
      • Jan 2008
      • 170

      #3
      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

      Comment

      Working...