Replace all special characters except asterisk

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vavc1980
    New Member
    • Feb 2008
    • 25

    Replace all special characters except asterisk

    Hello,

    I've been trying to find a way using regex to replace all special characters and spaces in a string but leaving the asterisk there.
    Example:
    (8.88)***1 234--

    result should be:
    888***1234

    I've been looking on the web (still am) and also trying myself but I just can't find it, what I have removes the asterisk too, which I don't want. Here's what i have (by the way this is being coded in C#)

    text = Regex.Replace(t ext, @"[()-. ]", string.Empty);

    Maybe it's something really simple, don't get mad at me regex it's really confusing to me, and like I said I've been trying to find the answer but without any success so far.

    Thanks.
  • vavc1980
    New Member
    • Feb 2008
    • 25

    #2
    Well, I kept trying and I was able to accomplish my goal, this is how I did it, maybe there is a better, cleaner way, but at least it works. If you have any better suggestions let me know.

    text = Regex.Replace(t ext, @"([\s-.()])", string.Empty);

    Comment

    Working...