sub

Search string for matches with regular expression pattern with attributes. Replace each match with string generated from format.

  1. string sub(string s, string pattern, string format, string attributes)
    string
    sub
    (
    string s
    ,
    string pattern
    ,
    string format
    ,
    string attributes = null
    )
  2. string sub(string s, string pattern, string delegate(RegExp) dg, string attributes)

Parameters

s string

String to search.

pattern string

Regular expression pattern.

format string

Replacement string format.

attributes string

Regular expression attributes.

Return Value

Type: string

the resulting string

Examples

Replace the letters 'a' with the letters 'ZZ'.

s = "Strap a rocket engine on a chicken."
sub(s, "a", "ZZ")        // result: StrZZp a rocket engine on a chicken.
sub(s, "a", "ZZ", "g")   // result: StrZZp ZZ rocket engine on ZZ chicken.

The replacement format can reference the matches using the $&, $$, $', $`, $0 .. $99 notation:

sub(s, "[ar]", "[$&]", "g") // result: St[r][a]p [a] [r]ocket engine on [a] chi

Meta