Re: search for exact string
by amarquis (Curate) on Mar 27, 2008 at 12:45 UTC
|
Well, one issue here is that the exact string "String" exists in the strings "String.c" and "String.o"
So, are you looking for the case where String is the only thing on a given line? If so, you want to look into the "start of line" and "end of line." Or are you looking for lines where " String " exists, separated from other content by whitespace? If so, you want to look into character classes (which can match something like, say, any non-word character)
Both of these cases are covered in perldoc perlretut, or if you are looking for some other kind of match you can clarify your question here.
Edit: I just read your question again, are you looking for any line that matches "String" except for the specific cases of String.c and String.o? You can do another pass after you've matched "String," and skip to the next iteration of the loop if you can match "String\.co"
| [reply] [d/l] |
|
|
Actually im looking for any line containing String followed by whitespace
| [reply] |
|
|
This will find a case-insensitive "sTRinG" followed by whitespace, even if the string is at the end of the line:
#!/usr/bin/env perl
use warnings;
use strict;
my $toFind = 'String';
while (<DATA>) {
print if /\b$toFind\s/i;
}
__DATA__
this is a string
strings are us
String.c is a file
so string.o is too
I like a good STRING now and again
This prints:
this is a string
I like a good STRING now and again
| [reply] [d/l] [select] |
|
|
for (@list){
### This will not match 'String' at the end of the line
print if /String\s/;
}
Unless you have a really unusual requirement, the typical thing to do in these cases is to look for the string followed by (or, even more commonly, surrounded by) a boundary or boundaries. Note that these are zero-width assertions - unlike whitespace which actually requires a character:
for (@list){
print if /String\b/;
}
This will match your string even if it's at the end of the line.
| [reply] [d/l] [select] |
Re: search for exact string
by Punitha (Priest) on Mar 27, 2008 at 12:51 UTC
|
use strict;
my $toFind="String";
while(<DATA>){
my $line=$_;
chomp($line);
if($line=~m/\b$toFind(?=[^\w\.]+|$)/i){
print "$line\n";
}
}
__DATA__
blah blah string blah
important stringstuff we wish to keep
Unimportant string
more stuff we wish to String.o
Some more unimportant String.c
Punitha | [reply] [d/l] [select] |
|
|
I'm afraid that your solution is incorrect; "/\b$toFind(?=[^\w\.]+|$)/i" means "match, case-insensitively, a word boundary (1) followed by the content of $toFind followed by (2) EITHER one or more characters which are not 'word-building' characters or literal periods OR the end of the line" - which does not match the specification that was asked for. That is, (1) was never specified (although it was implied) and (2) is simply wrong: you used a positive look-ahead assertion where you should have used a negative look-ahead. As a result, your regex will match, e.g., 'String-' or 'string*'.
| [reply] |
|
|
I copy pasted if($line=~m/\b$toFind(?=^\w\.+|$)/i) and it worked for my requirement... Thank you so much :)
I will be thankfull if you also can explain how it work?
| [reply] |
|
|
Anonymous Monk:
If you'd just read the other reply to the node you've resurrected, it would tell you what the regular expression means.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] |
Re: search for exact string
by oko1 (Deacon) on Mar 27, 2008 at 13:49 UTC
|
Noting specifically that you have asked to match for the exact string "String" (rather than a case-insensitive match), this should work:
#!/usr/bin/perl -w
use strict;
my $toFind="String";
for (<DATA>){
print if /$toFind(?!\.[oc])\b/;
}
__DATA__
this is a String
strings are us
String.c is a file
so string.o is too
I like a good STRING now and again
| [reply] [d/l] |
Re: search for exact string
by locked_user sundialsvc4 (Abbot) on Mar 27, 2008 at 15:03 UTC
|
It is, of course, perfectly reasonable to write an if statement with more than one regular-expression test in it ... using or.
Pay not the slightest attention to concerns of “efficiency,” for there are none.
| |