How to search a regex without replacing it in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vipul03
    New Member
    • Feb 2009
    • 2

    How to search a regex without replacing it in perl

    I want to search a regex in perl without replacing it and without going line by line to search it

    Any help will be apriciable

    Thanks in advance
  • Icecrack
    Recognized Expert New Member
    • Sep 2008
    • 174

    #2
    Question:
    What have your tried so far?

    How can we work with what you want, if we do not know the information we are looking for?

    Thanks

    Charlie

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      You can't search a regexp, the question makes no sense. You can search a file or a database, but you can't search a regexp.

      Comment

      • Vipul03
        New Member
        • Feb 2009
        • 2

        #4
        I have tried to serch each and every line in the file using

        Code:
        open FH,"log";
        for $line(<FH>){
        if( $line =~ m/regexp/ )
        {
        }
        }
        but i dont want to look each and every line in the file

        Thanks
        Last edited by eWish; Feb 3 '09, 07:46 PM. Reason: Please use the code tags

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Originally posted by Vipul03
          I have tried to serch each and every line in the file using

          open FH,"log";
          for $line(<FH>){
          if( $line =~ m/regexp/ )
          {
          }
          }

          but i dont want to look each and every line in the file

          Thanks
          If you don't try and match against every line, how are you going to have a proper search? Inside of the if statement, if you don't do anything, like print the line or the line number, then you will never know that there was a match.

          Regards,

          Jeff

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            maybe this is what you want, hard to say because I guess you think we can read your mind and understand your vague question:

            Code:
            open FH,"log";
            while (my $line = <FH>){
               if( $line =~ m/regexp/ ) {
                  "Found regexp in $line\n";
                  last; $stops searching after first match
               }  
            }
            close FH;
            If thats not what you are trying to do then please explain yourself properly so other people can understand what your requirements are.

            Comment

            • Icecrack
              Recognized Expert New Member
              • Sep 2008
              • 174

              #7
              Originally posted by KevinADC
              maybe this is what you want, hard to say because I guess you think we can read your mind and understand your vague question:

              Code:
              open FH,"log";
              while (my $line = <FH>){
                 if( $line =~ m/regexp/ ) {
                    "Found regexp in $line\n";
                    last; $stops searching after first match
                 }  
              }
              close FH;
              If thats not what you are trying to do then please explain yourself properly so other people can understand what your requirements are.
              I know in the quick making of that script you may of made a mistake or typo at
              Code:
              $stops Searching

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by Icecrack
                I know in the quick making of that script you may of made a mistake or typo at
                Code:
                $stops Searching
                Yes thanks, should have been "#stops searching" instead of "$stops Searching".

                Comment

                Working...