Advertisement
chotoipho

ch9_case_shifting.pl

Dec 18th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.95 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. # \U replaces matched pattern with all Upper Case
  5. $_ = "I saw Fred with Barney.";
  6. print "\$_ = '$_'\n";
  7. print 'Substitution Pattern =  s/(fred|barney)/\U$1/gi',"\n";
  8. s/(fred|barney)/\U$1/gi;
  9. print "\$_ = '$_'\n";
  10.  
  11. print "-" x 50, "\n";
  12.  
  13. # \L replaces matched pattern with all Lower Case
  14. $_ = "I saw Fred with Barney.";
  15. print "\$_ = '$_'\n";
  16. print 'Substitution Pattern =  s/(fred|barney)/\L$1/gi',"\n";
  17. s/(fred|barney)/\L$1/gi;
  18.  
  19. print "\$_ = '$_'\n";
  20.  
  21. print "-" x 50, "\n";
  22.  
  23. # \E turns off case shifting
  24. $_ = "I saw Fred with Barney.";
  25. print "\$_ = '$_'\n";
  26. print 'Substitution Pattern =  s/(\w+) with (\w+)/\U$2\E with $1/i',"\n";
  27. s/(\w+) with (\w+)/\U$2\E with $1/i;
  28. print "\$_ = '$_'\n";
  29.  
  30. print "-" x 50, "\n";
  31.  
  32. # \E turns off case shifting. Without it, 'BARNEY WITH FRED' will be all capitalized.
  33. $_ = "I saw Fred with Barney.";
  34. print "\$_ = '$_'\n";
  35. print 'Substitution Pattern =  s/(\w+) with (\w+)/\U$2 with $1/i',"\n";
  36. s/(\w+) with (\w+)/\U$2 with $1/i;
  37. print "\$_ = '$_'\n";
  38.  
  39. print "-" x 50, "\n";
  40.  
  41. # \u replaces the next character and changes it to Upper Case. Used for capitalizing the beginning of a word.
  42. $_ = "I saw fred with barney.";
  43. print "\$_ = '$_'\n";
  44. print 'Substitution Pattern =  s/(fred|barney)/\u$1/gi',"\n";
  45. s/(fred|barney)/\u$1/gi;
  46. print "\$_ = '$_'\n";
  47.  
  48. print "-" x 50, "\n";
  49.  
  50. # \l replaces the next character and changes it to Lower Case. Used for uncapitalizing the beginning of a word.
  51. $_ = "I saw FRED with BARNEY.";
  52. print "\$_ = '$_'\n";
  53. print 'Substitution Pattern =  s/(fred|barney)/\l$1/gi',"\n";
  54. s/(fred|barney)/\l$1/gi;
  55. print "\$_ = '$_'\n";
  56.  
  57. print "-" x 50, "\n";
  58.  
  59. # We can stack these backslash escapes. \u with \L means "all lowercase, but capitalize the first letter."
  60. $_ = "I saw fRED with bArNeY.";
  61. print "\$_ = '$_'\n";
  62. print 'Substitution Pattern =  s/(fred|barney)/\u\L$1/gi',"\n";
  63. s/(fred|barney)/\u\L$1/gi;
  64. print "\$_ = '$_'\n";
  65.  
  66. print "done.\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement