-3

Below is the example -

I'm going to ??Location??. I will go to ??Location_New??

In these string values I need to capture the text surrounded by ?? mark. I can replace it if the values within the ?? mark is static using contains method. However it is not but I need to capture the text. So can you someone guide me on this.

Application purpose is, It should capture any values surrounded by ?? mark and prompt to user then user will enter the value that should replace the text surrounded by ?? mark. Not just one text value only. All the text surrounded with ?? mark

Hezron Naresh
  • 79
  • 2
  • 12
  • See also http://stackoverflow.com/questions/20701818/how-to-replace-the-text-between-two-characters-in-c-sharp and so on. Read [ask] and share your research. – CodeCaster Oct 14 '16 at 06:57
  • @CodeCaster All those answers are explaining how to replace the one text value. How that should be helpful for me to replace multiple values in the paragraph. I need to get all the values surrounded by ?? mark. It can be more than 1 – Hezron Naresh Oct 14 '16 at 08:14
  • A regular expression can have multiple matches in one string. The point is that you're not the first who wants to replace some delimited substring, so try searching first and show what you have tried. – CodeCaster Oct 14 '16 at 08:32
  • Thanks for the advice on this. – Hezron Naresh Oct 14 '16 at 08:41
  • 1
    @CodeCaster Actually tried my self and found solution for this. http://rubular.com/r/JGhHTHbTgK – Hezron Naresh Oct 14 '16 at 08:42

1 Answers1

1

User System.Text.RegularExpressions.Regex

string eml = "??Location??";
string pattern = @"(?<=[?]{2})[\w]+(?=[?]{2})";
string result = System.Text.RegularExpressions.Regex.Replace(eml,pattern, m => m + "_new");

(?<=[?]{2}) text starts with ??

[\w]+ text to replace (one or many word character, alphanumeric & underscore)

(?=[?]{2}) text endswith ??

fubo
  • 44,811
  • 17
  • 103
  • 137