The s Operator


Introduction

The s operator searches a string for a search pattern, and replaces that pattern with a user chosen alternative, a replace pattern. The $instring is searched and then replaced according to the regular expression rules.

Examples for Hackers

We need to get familiar with the scheme used by trying examples. Here are some, of varying sophistication, which most likely is overkill, but... To see what's going on, the easiest is to type (or cut and paste) the following entries into the appropriate s Query boxes, Submit them, see what happens, and then change the entries slightly and see if what happens upon submission is what you expect.
instringsearch patternreplace patternExplanation
How are you?how are youI'm fineno match possible
How are you?how are youI'm fineusing ignore, watch the question mark
abracadabraaAusing global
From the point of view of Computer Assisted Testing/Homework, there are several important cases of replacement which will have to be addressed. First, one needs to change student's inputted text for the 'raised to a power' symbol, which is usually a caret, to the Perl form, '**'.
instringsearch patternreplace patternExplanation
7^(1/2)\^**notice the escape
The escape is needed before the caret, since the caret itself has a meaning in regular expressions.