Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

Please Help me in creating regular expression

please tell me what will be the regex for replacing values "_$$12" from a string where "12" can be any digit. i have tried with various combination but '$' is creating issue.

Upvotes: 0

Views: 69

Answers (5)

JSJ
JSJ

Reputation: 5691

try this.

        string input = "_$$12";
        string output = Regex.Replace(input, @"_\$\$", string.Empty);

the output will be 12. and if you increase the input something like "_$$123456" then the output will be 123456

Upvotes: 0

Ωmega
Ωmega

Reputation: 43703

Check this:

using System.Text.RegularExpressions;

class RegExSample 
{
  static void Main() 
  {
    string text = "text _$$12 text";
    string result = Regex.Replace(text, @"_\$\$\d+", "#replacement#");
    System.Console.WriteLine("result = [" + result + "]");
  }
}

See this code in action here.

Upvotes: 0

J0HN
J0HN

Reputation: 26961

$ is has special meaning in regexes, it marks the end of the string. E.g.

Regex.Replace(input_string,@"_\$\$\(d+)", @"\1");

Will replace _$$12 with just 12.

Upvotes: 2

Oded
Oded

Reputation: 499372

Since $ has special meaning in regular expressions, you need to escape it:

@"_\$\$\d\d"

Upvotes: 3

James
James

Reputation: 82136

var sanitized = Regex.Replace("_$$12", @"_\$\$[0-9]+", "ReplacementString");

Upvotes: 1

Related Questions