Site Tools


sar

# $EPIC: sar.txt,v 1.2 2006/08/19 09:25:52 sthalik Exp $

Synopsis:

$sar([c][i][g][r]/<search>/<replace>/<text>)

Technical:

The arguments are composed of:

  1. zero or more of the options c, i, g, and r,
  2. a delimiter which can be any character other than c, i, g, or r, and is customarily a forward-slash,
  3. some text (<search>) that may not contain the delimiter to be removed,
  4. a delimiter; the same as (2),
  5. some text (<replace>) that may not contain the delimiter to be inserted in place of #3,
  6. a delimiter; the same as (2), and
  7. a string of text (<text>) that MAY contain the delimiter to be transformed.

In its simple form, the string <text> is returned with the first instance of the string <search> replaced with the string <replace>. The search of <search> is case insensitive.

The options modify this behavior:

c The search for <search> should be case-sensitive.
i The search for <search> should be case-insensitive (this is the default, and the option is only included for backwards-compatibility).
g All instances of <search> should be replaced with <replace>. You must be careful that the contents of <replace> do not match <search> because this would cause it to be infinitely replaced.
r The <text> argument is the name of a variable. The search and replace is performed on the value of this variable, and the result is assigned back to the variable.

Practical:

This is the general purpose search-and-replace function. It allows you to look for any arbitrary text substring in any text string, and replace it with another arbitrary substring. Any of the strings may consist of variables to expand at runtime.

Returns:

resultant string

Examples:

@ foo = [foobarblah]
$sar(/oo/ee/booyamon)              returns "beeyamon"
$sar(/oo/ee/foofoo)                returns "feefoo"
$sar(g/oo/ee/foofoo)               returns "feefee"
$sar(r/oo/ee/foo)                  returns and sets $foo to "feebarblah"
sar.txt · Last modified: 2014/10/27 09:40 by 127.0.0.1