Jin Joe February 2016

Regex help for this expression with digits wanted

I am splitting given text wherever eachDELETEDDELETED occours, however some of my files contain text like:

each2,DELETED6,DELETED
eachDELETED2,DELETED
each5,DELETED15,DELETED
each5,DELETED5,DELETED2

I want to do a regex replace and turn these expressions into eachDELETEDDELETED.

I have tried using the follow code:

Regex ra = new Regex(@"eachDELETED\d, DELETED");

MatchCollection mcMatches = ra.Matches(extracted);

foreach (Match m in mcMatches)
{
    if (m.Success)
    {
        //  MessageBox.Show(m.Value.ToString());
        richTextBox5.Text += "JJJJ------>" +m.Value + "\n";
    }
}

But I'm not getting any matches.

Answers


NikolaiDante February 2016

The regex each\d*,*DELETED\d*,DELETED\d* matches all the sample data:

each2,DELETED6,DELETED
eachDELETED2,DELETED
each5,DELETED15,DELETED
each5,DELETED5,DELETED2

If the lack of the comma in the second line is a typo, use each\d*,DELETED\d*,DELETED\d*

Basically, \d matches a digit and * means zero or more times.

Post Status

Asked in February 2016
Viewed 3,034 times
Voted 8
Answered 1 times

Search




Leave an answer