0

This is my first post in this forum. I have text with in double which i want to replace by another text.

This is my full text

"Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor~9999"*"Operational Metrics~Canadian Dollars (Canadian $/US$)~9999"

This text "Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor~9999" will be replace with excel cell address A10.

I have tried this way but not being able to replace the text within double quote. surely there is problem in my code which i am not able to fix.

string mainstr = "\"" + "Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor~9999" + "\"" + "*" + "\"" + "Operational Metrics~Canadian Dollars (Canadian $/US$)~9999" + "\"";
string replacesfrom = "\"" + "Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor~9999" + "\"";
string replacesto = "A10";
string afterreplace = Regex.Replace(mainstr, replacesfrom, replacesto);

Desired output will be.

"A10"*"Operational Metrics~Canadian Dollars (Canadian $/US$)"

I tried this logic too but still no luck.

public static string ReplaceWholeWord(this string original, string wordToFind, string replacement, RegexOptions regexOptions = RegexOptions.None)
        {
            string pattern = String.Format(@"\b{0}\b", wordToFind);
            string ret = Regex.Replace(original, pattern, replacement, regexOptions);
            return ret;
        }

with above function i try to replace but unfortunately not being able to replace text Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor with A10

Please suggest what to change in my above code.

  • You don't need Regex here because you are simply replacing a static substring; `string.Replace` should suffice. If you must use Regex you need to be aware that parenthesis and the dollar and forward slash characters have special meaning and need to be escaped. – Johnathan Barclay Nov 13 '20 at 10:37

3 Answers3

1

Regex.Replace works differently than String.Replace. Use

string afterreplace = mainstr.Replace(replacesfrom, replacesto);
  • +1, better than my answer which is unnecessarily using Regex... also, [here](https://dotnetfiddle.net/EOYcLx)'s a link to a dotnetfiddle showing this works – MindSwipe Nov 13 '20 at 10:39
  • Replace may create problem. i need to replace text with in double quote. suppose there could be text like "Z1"+"Z12" replace will replace Z1 with A10. then output would be "A10"+"A102" this is why i do not use replace function. i need to find out text with in double quote replace. replace function may not work some time & create problem. – Sudip Bhattacharjee Nov 13 '20 at 10:43
0

first things first, you don't need the "+" in your strings ecxample:

string replacesfrom = "\"" + "Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor" + "\"";

could be:

string replacesfrom = "\"Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor\"";

And what I could understand, this is what you need, to replace:

string afterreplace = mainstr.Replace(replacesfrom, replacesto)

And to get cell adress, if you don't know how, this should work: How can i get the Cell address from excel

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

If you desperately want to use Regex, continue reading. If not, see Yaroslav's answer


The problems are that you havenescaped Regex special characters, i.E (, ), $, and /

Your current Regex is Operational Metrics~Copper C1 Cash Costs (US$/lb) - Lalor~9999 contains special characters (Regexr explains them better), basically ( and ) denote a capturing group and $ denotes the end of your string. Meaning we'll have to escape them with a \. That means your Regex (replacesfrom) is now

@"""Operational Metrics~Copper C1 Cash Costs \(US\$\/lb\) - Lalor~9999"""

Here is a dotnetfiddle showing that it works

MindSwipe
  • 7,193
  • 24
  • 47
  • thanks for your fiddle. how can i escape all special character the way you hard code in text that is `string replacesfrom = @"""Operational Metrics~Copper C1 Cash Costs \(US\$\/lb\) - Lalor~9999""";` – Sudip Bhattacharjee Nov 13 '20 at 10:52
  • $ should be replace with \$\ ? bracket should be replace with \( & \) ? – Sudip Bhattacharjee Nov 13 '20 at 10:52
  • No. Escaping in a Regex is done the same as escaping inside a normal string, just a single `\\` followed by the character you want to escape. I.E `\"` escapes a `"` inside a string, and `\$` escapes the `$` inside the Regex. I just used the verbatim string (`@"..."`) so I don't have to escape the escape (\\) character. I escaped the charactes `(`, `$`, `/`, and `)` separately – MindSwipe Nov 13 '20 at 10:55
  • How could i have generic routine which i will call to replace all special character. please suggest a code example or any link which guide me to replace special characters. thanks – Sudip Bhattacharjee Nov 13 '20 at 11:58
  • A quick Google search yielded me [`Regex.Escape`](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.escape?view=net-5.0) – MindSwipe Nov 13 '20 at 12:00