Connecting Tech Pros Worldwide Forums | Help | Site Map

How to search a regex without replacing it in perl

Newbie
 
Join Date: Feb 2009
Posts: 2
#1: Feb 3 '09
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's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#2: Feb 3 '09

re: How to search a regex without replacing it in perl


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
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Feb 3 '09

re: How to search a regex without replacing it in perl


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.
Newbie
 
Join Date: Feb 2009
Posts: 2
#4: Feb 3 '09

re: How to search a regex without replacing it in perl


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

Expand|Select|Wrap|Line Numbers
  1. open FH,"log";
  2. for $line(<FH>){
  3. if( $line =~ m/regexp/ )
  4. {
  5. }
  6. }
but i dont want to look each and every line in the file

Thanks
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,569
#5: Feb 3 '09

re: How to search a regex without replacing it in perl


Quote:

Originally Posted by Vipul03 View Post

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
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#6: Feb 3 '09

re: How to search a regex without replacing it in perl


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

Expand|Select|Wrap|Line Numbers
  1. open FH,"log";
  2. while (my $line = <FH>){
  3.    if( $line =~ m/regexp/ ) {
  4.       "Found regexp in $line\n";
  5.       last; $stops searching after first match
  6.    }  
  7. }
  8. close FH;
  9.  
If thats not what you are trying to do then please explain yourself properly so other people can understand what your requirements are.
Icecrack's Avatar
Expert
 
Join Date: Sep 2008
Location: Sydney, Australia
Posts: 173
#7: Feb 3 '09

re: How to search a regex without replacing it in perl


Quote:

Originally Posted by KevinADC View Post

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

Expand|Select|Wrap|Line Numbers
  1. open FH,"log";
  2. while (my $line = <FH>){
  3.    if( $line =~ m/regexp/ ) {
  4.       "Found regexp in $line\n";
  5.       last; $stops searching after first match
  6.    }  
  7. }
  8. close FH;
  9.  
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
Expand|Select|Wrap|Line Numbers
  1. $stops Searching
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#8: Feb 3 '09

re: How to search a regex without replacing it in perl


Quote:

Originally Posted by Icecrack View Post

I know in the quick making of that script you may of made a mistake or typo at

Expand|Select|Wrap|Line Numbers
  1. $stops Searching

Yes thanks, should have been "#stops searching" instead of "$stops Searching".
Reply