Regarding the possible dupe post: Replace only some groups with Regex
This is not a dupe as the post replaces the group with static text, what I want is to replace the group by retaining the text in the group.
I have some texts which contain pattern like:
\super 1 \nosupersub
\super 2 \nosupersub
...
\super 592 \nosupersub
I want to replace them using regex such that they become:
<sup>1</sup>
<sup>2</sup>
...
<sup>592</sup>
So, I am using the following regex (note the group (\d+)
):
RegexOptions options = RegexOptions.Multiline; //as of v1.3.1.0 default is multiline
mytext = Regex.Replace(mytext, @"\s?\\super\s?(\d+)\s?\\nosupersub\s", @"<sup>\1</sup>", options);
However, instead of getting what I want, I got all the results replaced with <sup>\1</sup>
:
<sup>\1</sup>
<sup>\1</sup>
...
<sup>\1</sup>
If I try the regex replacement using a text editor like https://www.sublimetext.com and also using Python
, it is OK.
How to get such group replacement of (\d+)
like that (retain the number) in C#
?