in reply to search for exact string

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"

Replies are listed 'Best First'.
Re^2: search for exact string
by gfarhat (Novice) on Mar 27, 2008 at 12:55 UTC
    Actually im looking for any line containing String followed by whitespace
      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

      That's possible - but has built-in problems.

      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.