regex - Perl regular expression not responding to $ -


i have following code:

#!/usr/bin/perl use test::simple tests => 2;     $file = "lalaletc"; $file2 = "lalal"; ok($file =~ m/^lala/); ok($file2 =~ m/^lala$/); 

the output following:

1..2 ok 1 not ok 2 #   failed test @ ./test.pl line 7. # looks failed 1 test of 2. 

but $file2 has no more characters, expected succeed test 2. why not?

your variable $file2 set lalal. 5 characters. regex /^lala$/, means:

  • beginning of line/string
  • lala
  • end of line/string

that's 4 characters because last l missing. regex not match , test fails. correct behaviour.

try here: http://rubular.com/r/o0gheiivaz add missing l @ end , match.

here's additional explanation taken this site:

node                     explanation --------------------------------------------------------------------------------   ^                        beginning of string --------------------------------------------------------------------------------   lala                     'lala' --------------------------------------------------------------------------------   $                        before optional \n, , end of                            string 

note case, not need regular expression. can use eq operator.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -