-2
string formattedFormula = Regex.Replace("A1+A1", "(?!A1\\d+)[A1]" , "{" + 0 + "}");

I need the result as {0}+{0}. But this code replaced like this {0}{0}+{0}{0}

this is just an example.

using System;
using System.Text.RegularExpressions;

public class HelloWorld
{
    public static void Main(string[] args)
    {
       string formattedFormula = Regex.Replace("A1+A1", "(?!A1\\d+)[A1]" , "{" + 0 + "}");
        Console.WriteLine (formattedFormula);
    }
}

My Real code is

foreach (string columnCode in parameters)
            {
                string pattern = string.Empty;

                if (!Common.Common.IsNumaric(columnCode))
                {
                    pattern = "(?!" + columnCode + "\\d+)[" + columnCode + "]";

                    stringList.Add(columnCode);
                    incrementor++;

                    formattedFormula = Regex.Replace(formattedFormula, pattern, "{" + incrementor.ToString() + "}");
                }
                else
                {
                    continue;
                }
            }

enter image description here

Dev006
  • 87
  • 1
  • 13
  • Please add code and data as text ([using code formatting](/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – gunr2171 Jun 08 '22 at 01:37
  • Does this _need_ to be regex? Is your input always "Letter Number Plus Letter Number"? – gunr2171 Jun 08 '22 at 01:38
  • Why are you always printing "0", and always matching specifically "A1"? – gunr2171 Jun 08 '22 at 01:48
  • @gunr2171 This is just an example. – Dev006 Jun 08 '22 at 01:51
  • You didn't answer my first two questions, so I'm going to assume that your input format is as I described, your expected output for your example is `{A1}+{A1}`, and regex is not needed. – gunr2171 Jun 08 '22 at 01:56
  • @gunr2171 my expected output is {0}+{0} – Dev006 Jun 08 '22 at 02:03
  • So then `return "{0}+{0}";` is all the code you need. Please, be more clear about your inputs and outputs. I have no idea what you're actually trying to accomplish. – gunr2171 Jun 08 '22 at 02:10
  • @gunr2171 I have updated with the real code. – Dev006 Jun 08 '22 at 02:19
  • Try `pattern = $@"\b(?!{columnCode}\d){columnCode}\b";` – Wiktor Stribiżew Jun 08 '22 at 08:14

2 Answers2

1

You can use

pattern = $@"\b(?!{columnCode}\d){columnCode}\b";

See the resulting regex demo. It matches

  • \b - a word boundary
  • (?!A1\d) - fail the match if there is A1 + a digit
  • A1 - a fixed text
  • \b - a word boundary.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
-1

This pattern worked for my Scenario.

pattern = "(?:\\d+["+ columnCode + "]|["+ columnCode + "]+\\d)["+ columnCode + "\\d]*";
Dev006
  • 87
  • 1
  • 13