[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

Remove numbers from string regex

I need to remove numbers from strings like

Data1

Date3Last

so that i end up with

Data and DateLast


Using c# regex...
0
JonMny
Asked:
JonMny
1 Solution
 
ddrudikCommented:

using System;
using System.Text.RegularExpressions;
namespace myapp
{
  class Class1
    {
      static void Main(string[] args)
        {
          String sourcestring = "Data123";
          String matchpattern = @"\d+";
          String replacementpattern = @"";
          Console.WriteLine(Regex.Replace(sourcestring,matchpattern,replacementpattern));
        }
    }
}

Open in new window

0
 
Fernando SotoCommented:
Hi JonMny;

When using the Regex.Replace method you need to assign the return value of that function to a variable or itself as shown below.

List<string> stripdigits = new List<string>();
stripdigits.Add("Data1");
stripdigits.Add("Date3Last");

for (int idx = 0; idx < stripdigits.Count; idx++)
{
    stripdigits[idx] = Regex.Replace(stripdigits[idx], @"\d+", String.Empty);
}

Fernando
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now