in reply to Re: search for exact string
in thread search for exact string

Actually im looking for any line containing String followed by whitespace

Replies are listed 'Best First'.
Re^3: search for exact string
by toolic (Bishop) on Mar 27, 2008 at 13:19 UTC
    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
Re^3: search for exact string
by oko1 (Deacon) on Mar 27, 2008 at 14:50 UTC

    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.