How to Remove Numbers from a String with Regex

How to remove numbers from string using Regex.Replace?

Try the following:

var output = Regex.Replace(input, @"[\d-]", string.Empty);

The \d identifier simply matches any digit character.

How to remove numbers from a string?

Very close, try:

questionText = questionText.replace(/[0-9]/g, '');

replace doesn't work on the existing string, it returns a new one. If you want to use it, you need to keep it!

Similarly, you can use a new variable:

var withNoDigits = questionText.replace(/[0-9]/g, '');

One last trick to remove whole blocks of digits at once, but that one may go too far:

questionText = questionText.replace(/\d+/g, '');

Java: removing numeric values from string

Your regular expression [^A-Z] is currently only configured to preserve upper-case letters. You could try replacing it with [^A-Za-z] to keep the lower-case letters too.

How to remove numbers from end of the string Regex Javascript

This extracts the text between the AM/PM and the trailing digits.

string.replace(/^.*\d{1,2}:\d\d\s*[AP]M(.*?)\d*$/, "$1");

Removing numbers at the end of a string C#

Try this:

string input = "123ABC79";
string pattern = @"\d+$";
string replacement = "";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

Putting the $ at the end will restrict searches to numeric substrings at the end. Then, since we are calling Regex.Replace, we need to pass in the replacement pattern as the second parameter.

Demo

What regular expression would allow me to remove numbers up until english characters start? (python)

I suggest splitting the string into two chunks with ' - ' substring, and grab the last chunk:

result = myString.split(' - ', 1)[-1]

See a Python demo:

texts = ['#1 - New York', '#4 - London']
for myString in texts:
print( myString, '=>', myString.split(' - ', 1)[-1] )

Output:

#1 - New York => New York
#4 - London => London

Regarding the regex solution, you might want to remove any non-letters at the start of the string with re.sub(r'^[\W\d_]+', '', myString) or re.sub(r'^[^a-zA-Z]+', '', myString). Note [\W\d_]+ is a fully Unicode aware pattern while ^[^a-zA-Z]+ is ASCII only.

Strip all non-numeric characters from string in JavaScript

Use the string's .replace method with a regex of \D, which is a shorthand character class that matches all non-digits:

myString = myString.replace(/\D/g,'');

Delete digits in Python (Regex)

Add a space before the \d+.

>>> s = "This must not b3 delet3d, but the number at the end yes 134411"
>>> s = re.sub(" \d+", " ", s)
>>> s
'This must not b3 delet3d, but the number at the end yes '

Edit: After looking at the comments, I decided to form a more complete answer. I think this accounts for all the cases.

s = re.sub("^\d+\s|\s\d+\s|\s\d+$", " ", s)

Removing Numbers and Errant Letters from string

Using the anchor ^ at beginning of string you can extract names.

Regex: ^(?:[A-Za-z]+\s)* with multi-line option ON

Explanation:

^ will start matching at beginning of line.
(?:[A-Za-z]+\s)* will look for combination of characters (assuming names don't have numbers in it) followed by a whitespace. This pattern will be search for multiple times since * quantifier is used. Thus matching more than one name in a line.

Regex101 Demo

regex remove all numbers except for the ones combined with alphabets

You can use word boundary RegEx

\b\d+\b

This will match one or more digits which are not surrounded by any alphabetical character and _ underscore.

Code:

myString = myString.replaceAll("\\b\\d+\\b", "");

Regex Demo



Related Topics



Leave a reply



Submit