Here are some handy uses of the match operator:
s/\^\s+//;
This pattern uses the \
s predefined character class to match any whitespace
character. The plus sign means to match zero or more white space characters, and
the caret means match only at the beginning of the string.
s/\s+$//;
This pattern uses the \
s predefined character class to match any whitespace
character. The plus sign means to match zero or more white space characters, and
the dollar sign means match only at the end of the string.
$prefix = "A"; s/^(.*)/$prefix$1/;
When the substitution is done, the value in the $prefix variable will be added to the beginning of the $_ variable. This is done by using variable interpolation and pattern memory. Of course, you also might consider using the string concatenation operator; for instance, $_ = "A" . $_;, which is probably faster.
$suffix = "Z"; s/^(.*)/$1$suffix/;
When the substitution is done, the value in the $suffix variable will be added to the end of the $_ variable. This is done by using variable interpolation and pattern memory. Of course, you also might consider using the string concatenation operator; for instance, $_ .= "Z";, which is probably faster.
s/^\s*(\w+)\W+(\w+)/$2 $1/;
This substitution statement uses the pattern memory variables $1 and $2 to reverse the first two words in a string. You can use a similar technique to manipulate columns of information, the last two words, or even to change the order of more than two matches.
s/\w/$& x 2/eg;}
When the substitution is done, each character in $_ will be repeated. If the original string was "123abc", the new string would be "112233aabbcc". The e option is used to force evaluation of the replacement string. The $& special variable is used in the replacement pattern to reference the matched string, which then is repeated by the string repetition operator.
s/\(w+)/\u$1/g;}
When the substitution is done, each character in $_ will have its first
letter capitalized. The /g option means that each word-the \
w+
meta-sequence-will be matched and placed in $1. Then it will be replaced
by \
u$1. The \
u will capitalize whatever follows it; in this case,
it's the matched word.
$_ = "!!!!"; $char = "!"; $insert = "AAA"; s{ ($char) # look for the specified character. (?=$char) # look for it again, but don't include # it in the matched string, so the next } # search also will find it. { $char . $insert # concatenate the specified character # with the string to insert. }xeg; # use extended mode, evaluate the # replacement pattern, and match all # possible strings. print("$_\n");
This example uses the extended mode to add comments directly inside the regular expression. This makes it easy to relate the comment directly to a specific pattern element. The match pattern does not directly reflect the originally stated goal of inserting a string between two repeated characters. Instead, the example was quietly restated. The new goal is to substitute all instances of $char with $char. $insert, if $char is followed by $char. As you can see, the end result is the same. Remember that sometimes you need to think outside the box.
s/(\\$\w+)/\$1/eeg;
This is a simple example of secondary variable interpolation. If $firstVar ="AAA" and $_ = '$firstVar', then $_ would be equal to "AAA" after the substitution was made. The key is that the replacement pattern is evaluated twice. This technique is very powerful. It can be used to develop error messages used with variable interpolation.
$errMsg = "File too large"; $fileName = "DATA.OUT"; $_ = 'Error: $errMsg for the file named $fileName'; s/(\$\w+)/$1/eeg; print;
When this program is run, it will display
Error: File too large for the file named DATA.OUT
The values of the $errMsg and $fileName variables were interpolated into the replacement pattern as needed.