Search code examples
c#regexroman-numerals

Replace matched characters from regex in a string


I'm trying to figure out how to replace some text in this string:

'some text blah blah XII'

I need to replace the Roman Numerals with an empty string, resulting in:

'some text blah blah'

I have the following regex which correctly matches a Roman Numeral.

string p1 = "^m*(d?c{0,3}|c[dm])"+ "(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$";

How do I replace the matches with an empty string?

UPDATE

i tried like this and it doesn't work

string algo = Regex.Replace("some text blah blah XII", "\bm*(d?c{0,3}|c[dm])(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])\b"," ");

Solution

  • The point is that your regular expression matches the whole string, because the regular expression starts with ^ (= beginning of the line/string) and ends with $ (= end of the line/string). To match a single word instead, replace the boundaries ^ and $ by word boundaries, \b.

    string p1 = "\bm*(d?c{0,3}|c[dm])(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])\b";
    

    Now the expression matches any isolated word that looks like a roman numeral, and this can be used to replace it.