How do I pass * characters in System.Text.RegularExpressions.Regex.Replace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sal Sal
    New Member
    • Nov 2010
    • 15

    #1

    How do I pass * characters in System.Text.RegularExpressions.Regex.Replace

    I am using System.Text.Reg ularExpressions .Regex.Replace( input, pattern, replacement, options) in c# to replace the input string. The pattern I am using may have one or more * or . or any other character basically inside mixed in with the rest of the string. I want to find this string in the input and replace it with something.

    For example say my string is *000000000

    I get the following error
    System.Argument Exception: parsing "*000000000 " - Quantifier {x,y} following nothing.

    How do I treat the * as a regular character?

    I tried replacing all instances of the * character with \* before passing it to replace function but in the final output I have the \ characters along with it...I do not want that.

    Thanks in advance for any help
  • Sal Sal
    New Member
    • Nov 2010
    • 15

    #2
    actually I found something that works

    I used Regex.Escape(pa ttern) to treat the * as a regular character. I pass that instead of pattern itself in the replace function.

    Comment

    Working...