The following code example demonstrates how the regular expression class
This code replaces all the digits in a string with underscores (_) and then replaces those with an empty string, effectively removing them. The same effect can be accomplished in a single step, but two steps are used here for demonstration purposes.
Example
| В | Copy Code |
|---|---|
// regex_replace.cpp
// compile with: /clr
#using <System.dll>
using namespace System::Text::RegularExpressions;
using namespace System;
int main()
{
String^ before = "The q43uick bro254wn f0ox ju4mped";
Console::WriteLine("original : {0}", before);
Regex^ digitRegex = gcnew Regex("(?<digit>[0-9])");
String^ after = digitRegex->Replace(before, "_");
Console::WriteLine("1st regex : {0}", after);
Regex^ underbarRegex = gcnew Regex("_");
String^ after2 = underbarRegex->Replace(after, "");
Console::WriteLine("2nd regex : {0}", after2);
return 0;
} | |
Dhtml editor
Free javascript download