3

I want to replace the text between the tags upcase to its uppercase version. Is there a way to do it by only using Regex.Replace method? (without using IndexOf)

Below is the code I was trying:

string texto = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";                
Console.WriteLine(Regex.Replace(texto, "<upcase>(.*)</upcase>", "$1".ToUpper()));

The expected result is:

We are living in YELLOW SUBMARINE. We don't have ANYTHING else.

but I get:

We are living in yellow submarine. We don't have anything else.
7
  • 3
    What's wrong with the above? try "<upcase>(.*?)</upcase>" Commented Dec 18, 2014 at 12:20
  • What is the output ? What do you expect ? Commented Dec 18, 2014 at 12:22
  • "We are living in a YELLOW SUBMARINE. We don't..." Commented Dec 18, 2014 at 12:25
  • I don't think you can do that with regular expressions. The ToUpper will apply to the "$1" and have no effect. Commented Dec 18, 2014 at 12:28
  • 1
    @vmp Actually you can do it, but you have to use the overload of Regex.Replace that takes a delegate that allows you to manipulate the matches. Just check out Avinash's answer. Commented Dec 18, 2014 at 12:34

1 Answer 1

7

I would do like,

string str = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";
string result = Regex.Replace(str, "(?<=<upcase>).*?(?=</upcase>)",  m => m.ToString().ToUpper());
Console.WriteLine(Regex.Replace(result, "</?upcase>", ""));

Output:

We are living in a YELLOW SUBMARINE. We don't have ANYTHING else.

IDEONE

Explanation:

  • (?<=<upcase>).*?(?=</upcase>) - Matches the text which was present inbetween <upcase> ,</upcase> tags. (?<=...) called positive lookbehind assertion, here it asserts that the match must be preceded by <upcase> string. (?=</upcase>) called positive lookahead which asserts that the match must be followed by </upcase> string. So the second line of code changes all the matched characters to uppercase and stores the result to the result variable.

  • /? optional / (forward slash). So the third line of code replaces all the <upcase> or </upcase> tags present in the result variable with an empty string and prints the final output.

Sign up to request clarification or add additional context in comments.

1 Comment

Would you have a link to a reference on that part? I got a little confused with that many interrogation marks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.