Skip to Main Content
Wyra?enia regularne. Receptury
book

Wyra?enia regularne. Receptury

by Jan Goyvaerts, Steven Levithan
May 2010
Intermediate to advanced content levelIntermediate to advanced
520 pages
17h 44m
Polish
Helion
Content preview from Wyra?enia regularne. Receptury

3.14. Zastępowanie wszystkich dopasowań

Problem

Chcemy zastąpić wszystkie dopasowania do wyrażenia regularnego <before> tekstem docelowym «after».

Rozwiązanie

C#

Jeśli chcemy przetworzyć niewielką liczbę łańcuchów z wykorzystaniem tego samego wyrażenia regularnego, możemy posłużyć się wywołaniem statycznym:

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

Jeśli korzystamy z wyrażenia regularnego wpisanego przez użytkownika końcowego, powinniśmy otoczyć to wywołanie statyczne kodem obsługującym ewentualne wyjątki:

string resultString = null;
try {
    resultString = Regex.Replace(subjectString, "before", "after");
} catch (ArgumentNullException ex) {
    // W roli wyrażenia regularnego, łańcucha do przetworzenia i tekstu docelowego
    // nie ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mikrousługi oparte na zdarzeniach

Mikrousługi oparte na zdarzeniach

Adam Bellemare

Publisher Resources

ISBN: 9781457167003