code_junkie

Why does this Perl regexp fail

Nov 13th, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. my $progName = shift ;
  2.  
  3. open(IPLAYERLIST, "iplayer-list.html") or die "Cannot open iplayer index file iplayer-list.htmln" ;
  4. while (<IPLAYERLIST>) {
  5.  
  6. if ( /($progName)/is ) {
  7. #if ( /Just A Minute/is ) { <-- This works!
  8. my $iplayerID = $1 ;
  9. print "IPlayer program id for $progName is $iplayerIDn" ;
  10.  
  11. # === do stuff here ===
  12. }
  13. else
  14. {
  15. print "Failed to match $progName in $_n";
  16. }
  17. }
  18.  
  19. #!/usr/bin/perl
  20. use strict ;
  21.  
  22. my $searchstr = "foo bar Just A Minute baz boo" ;
  23. my $progName = $ARGV[0] ;
  24. print "searching for [$progName] in [$searchstr]n" ;
  25. if ( $searchstr =~ /$progName/is ) {
  26. print "Well the test workedn" ;
  27. } else {
  28. print "Failed to match [$progName] in [$searchstr]n";
  29. }
  30.  
  31. Foo
  32. Just A Minute
  33. Bar
  34.  
  35. Failed to match Just A Minute in Foo
  36.  
  37. IPlayer program id for Just A Minute is Just A Minute
  38. Failed to match Just A Minute in Bar
  39.  
  40. my $progName = shift ;
  41.  
  42. open(IPLAYERLIST, "list.txt") or die "Cannot open iplayer index filen" ;
  43. while (<IPLAYERLIST>) {
  44.  
  45. if ( /($progName)/is ) {
  46. #if ( /Just A Minute/is ) { <-- This works!
  47. my $iplayerID = $1 ;
  48. print "IPlayer program id for $progName is $iplayerIDn" ;
  49.  
  50. # === do stuff here ===
  51. }
  52. else
  53. {
  54. print "Failed to match $progName in $_n";
  55. }
  56. }
  57.  
  58. egg
  59. spam
  60. foo bar Just A Minute baz boo
  61. egg spam Just A Minute spam egg
  62. foo
  63. bar
  64.  
  65. Failed to match just a minute in egg
  66.  
  67. Failed to match just a minute in spam
  68.  
  69. IPlayer program id for just a minute is Just A Minute
  70. IPlayer program id for just a minute is Just A Minute
  71. Failed to match just a minute in foo
  72.  
  73. Failed to match just a minute in bar
  74.  
  75. Just A Minute (extra spaces)
  76. Just
  77. A
  78. Minute
  79. Just <!--comment-->A Minute
  80. Just[the nbsp entity]A Minute
Add Comment
Please, Sign In to add comment