Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use strict;
- # \U replaces matched pattern with all Upper Case
- $_ = "I saw Fred with Barney.";
- print "\$_ = '$_'\n";
- print 'Substitution Pattern = s/(fred|barney)/\U$1/gi',"\n";
- s/(fred|barney)/\U$1/gi;
- print "\$_ = '$_'\n";
- print "-" x 50, "\n";
- # \L replaces matched pattern with all Lower Case
- $_ = "I saw Fred with Barney.";
- print "\$_ = '$_'\n";
- print 'Substitution Pattern = s/(fred|barney)/\L$1/gi',"\n";
- s/(fred|barney)/\L$1/gi;
- print "\$_ = '$_'\n";
- print "-" x 50, "\n";
- # \E turns off case shifting
- $_ = "I saw Fred with Barney.";
- print "\$_ = '$_'\n";
- print 'Substitution Pattern = s/(\w+) with (\w+)/\U$2\E with $1/i',"\n";
- s/(\w+) with (\w+)/\U$2\E with $1/i;
- print "\$_ = '$_'\n";
- print "-" x 50, "\n";
- # \E turns off case shifting. Without it, 'BARNEY WITH FRED' will be all capitalized.
- $_ = "I saw Fred with Barney.";
- print "\$_ = '$_'\n";
- print 'Substitution Pattern = s/(\w+) with (\w+)/\U$2 with $1/i',"\n";
- s/(\w+) with (\w+)/\U$2 with $1/i;
- print "\$_ = '$_'\n";
- print "-" x 50, "\n";
- # \u replaces the next character and changes it to Upper Case. Used for capitalizing the beginning of a word.
- $_ = "I saw fred with barney.";
- print "\$_ = '$_'\n";
- print 'Substitution Pattern = s/(fred|barney)/\u$1/gi',"\n";
- s/(fred|barney)/\u$1/gi;
- print "\$_ = '$_'\n";
- print "-" x 50, "\n";
- # \l replaces the next character and changes it to Lower Case. Used for uncapitalizing the beginning of a word.
- $_ = "I saw FRED with BARNEY.";
- print "\$_ = '$_'\n";
- print 'Substitution Pattern = s/(fred|barney)/\l$1/gi',"\n";
- s/(fred|barney)/\l$1/gi;
- print "\$_ = '$_'\n";
- print "-" x 50, "\n";
- # We can stack these backslash escapes. \u with \L means "all lowercase, but capitalize the first letter."
- $_ = "I saw fRED with bArNeY.";
- print "\$_ = '$_'\n";
- print 'Substitution Pattern = s/(fred|barney)/\u\L$1/gi',"\n";
- s/(fred|barney)/\u\L$1/gi;
- print "\$_ = '$_'\n";
- print "done.\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement