I need to use Regex.Replace
to remove all numbers and signs from a string.
Example input: 123- abcd33
Example output: abcd
To remove all numbers from a string, call the replace() method, passing it a regular expression that matches all numbers as the first parameter and an empty string as the second. The replace method will return a new string that doesn't contain any numbers.
Very close, try: questionText = questionText. replace(/[0-9]/g, '');
The REGEXREPLACE( ) function uses a regular expression to find matching patterns in data, and replaces any matching values with a new string. standardizes spacing in character data by replacing one or more spaces between text characters with a single space.
In Python, an inbuilt function sub() is present in the regex module to delete numbers from the Python string. The sub() method replaces all the existences of the given order in the string using a replacement string.
Try the following:
var output = Regex.Replace(input, @"[\d-]", string.Empty);
The \d
identifier simply matches any digit character.
You can do it with a LINQ like solution instead of a regular expression:
string input = "123- abcd33"; string chars = new String(input.Where(c => c != '-' && (c < '0' || c > '9')).ToArray());
A quick performance test shows that this is about five times faster than using a regular expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With