Back
Close

C# Professional - Processing Text

talent-agile
33.9K views
Previous: Regular Expressions - Groups & Capture

Regular Expressions - Replacing Text

As we saw before, regular expressions can specify groups in the pattern to capture data from the text.

But these groups can also be used for replacing data in a given text.

Note: Be careful when doing replacement, as the whole match will be replaced. Make sure you capture text that should be kept in the replacement if it is part of the pattern

// { autofold
using System;
using System.Text.RegularExpressions;
class Example
{
static void Main()
{
// }
// This pattern will match multiple spaces
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Backreferences can be used for replacement using the $N syntax, where N is the number of the captured group that you want to use for replacement.

If you want to use named capture groups, you need to use the following syntax ${groupname}.

// { autofold
using System;
using System.Text.RegularExpressions;
class Example
{
static void Main()
{
// }
// This pattern will extract user data from a csv text
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants