C#
string resultString = Regex.Replace(subjectString, "before", "after");
Jeli korzystamy z wyraenia regularnego wpisanego przez uytkownika kocowego, powinnimy otoczy to wywoanie statyczne kodem obsugujcym ewentualne wyjtki:
string resultString = null;
try {
    resultString = Regex.Replace(subjectString, "before", "after");
} catch (ArgumentNullException ex) {
    // W roli wyraenia regularnego, acucha do przetworzenia i tekstu docelowego
    // nie mona stosowa wartoci null.
} catch (ArgumentException ex) {
    // W wyraeniu regularnym wystpi bd skadniowy.
}

Regex regexObj = new Regex("before");
string resultString = regexObj.Replace(subjectString, "after");

string resultString = null;
try {
    Regex regexObj = new Regex("before");
    try {
        resultString = regexObj.Replace(subjectString, "after");
    } catch (ArgumentNullException ex) {
        // W roli acucha do przetworzenia i tekstu docelowego nie mona stosowa wartoci null.
    }
} catch (ArgumentException ex) {
    // W wyraeniu regularnym wystpi bd skadniowy.
}


VB.NET
Dim ResultString = Regex.Replace(SubjectString, "before", "after")

Dim ResultString As String = Nothing
Try
    ResultString = Regex.Replace(SubjectString, "before", "after")
Catch ex As ArgumentNullException
    'W roli wyraenia regularnego, acucha do przetworzenia i tekstu docelowego
    'nie mona stosowa wartoci null.
Catch ex As ArgumentException
    'W wyraeniu regularnym wystpi bd skadniowy.
End Try

Dim RegexObj As New Regex("before")
Dim ResultString = RegexObj.Replace(SubjectString, "after")

Dim ResultString As String = Nothing
Try
    Dim RegexObj As New Regex("before")
    Try
        ResultString = RegexObj.Replace(SubjectString, "after")
    Catch ex As ArgumentNullException
        'W roli acucha do przetworzenia i tekstu docelowego nie mona stosowa wartoci null.
    End Try
Catch ex As ArgumentException
    'W wyraeniu regularnym wystpi bd skadniowy.
End Try


Java
String resultString = subjectString.replaceAll("before", "after");

try {
    String resultString = subjectString.replaceAll("before", "after");
} catch (PatternSyntaxException ex) {
    // W wyraeniu regularnym wystpi bd skadniowy.
} catch (IllegalArgumentException ex) {
    // W tekcie docelowym wystpi bd skadniowy (na przykad symbol $ bez sekwencji ucieczki).
} catch (IndexOutOfBoundsException ex) {
    // W tekcie docelowym uyto nieistniejcego odwoania wstecz.
}

Pattern regex = Pattern.compile("before");
Matcher regexMatcher = regex.matcher(subjectString);
String resultString = regexMatcher.replaceAll("after");

String resultString = null;
try {
    Pattern regex = Pattern.compile("before");
    Matcher regexMatcher = regex.matcher(subjectString);
    try {
        resultString = regexMatcher.replaceAll("after");
    } catch (IllegalArgumentException ex) {
        // W tekcie docelowym wystpi bd skadniowy (na przykad symbol $ bez sekwencji ucieczki).
    } catch (IndexOutOfBoundsException ex) {
        // W tekcie docelowym uyto nieistniejcego odwoania wstecz.
    }
} catch (PatternSyntaxException ex) {
    // W wyraeniu regularnym wystpi bd skadniowy.
}


JavaScript
result = subject.replace(/before/g, "after");


PHP
$result = preg_replace('/before/', 'after', $subject);


Perl
s/before/after/g;

$subject =~ s/before/after/g;

($result = $subject) =~ s/before/after/g;


Python
result = re.sub("before", "after", subject)

reobj = re.compile("before")
result = reobj.sub("after", subject)


Ruby
result = subject.gsub(/before/, 'after')
