balgeary has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm having trouble getting my regular expressions to match more than one character. When I give it a file that contains the word "text", this will print "hi":

while ($line = <STDIN>){ if ($line =~ /t/){ print "hi"; } }

But this won't, even though I can see in my input file that there's a word "text"

while ($line = <STDIN>){ if ($line =~ /te/){ print "hi"; } }

Is this a bug, or am I doing something wrong in my code? I've tried uninstalling and reinstalling Strawberry Perl, and trying strings of characters that are in the input file, but it's still not working.

UPDATE: Hi! I figured out what it was -- the input file was encoded as UCS-2 Big Endian for some reason. Thank you for the responses!

Replies are listed 'Best First'.
Re: Regular expression only matches a single character
by stevieb (Canon) on Jun 11, 2015 at 21:40 UTC

    Given this code:

    #!/usr/bin/perl # std.pl use strict; use warnings; while (my $line = <STDIN> ){ if ($line =~ /te/){ print "hi"; } }

    ...and this input file:

    # std.txt a three text

    This: perl std.pl < std.txt works, as does running perl std.pl and entering text on the command line, on both Windows and Linux.

    How are you grabbing the file, and what does it contain?

    -stevieb

Re: Regular expression only matches a single character
by Eily (Monsignor) on Jun 11, 2015 at 21:34 UTC

    I'm guessing you might be parsing the file name instead of the file. Try

    while (my $line = <STDIN>) { print "Line: $line"; if ($line =~ /te/) { print "Match !\n"; } }

    Then you should read File Input and Output