How to Remove All Numbers from String

Removing numbers from string

Would this work for your situation?

>>> s = '12abcd405'
>>> result = ''.join([i for i in s if not i.isdigit()])
>>> result
'abcd'

This makes use of a list comprehension, and what is happening here is similar to this structure:

no_digits = []
# Iterate through the string, adding non-numbers to the no_digits list
for i in s:
if not i.isdigit():
no_digits.append(i)

# Now join all elements of the list with '',
# which puts all of the characters together.
result = ''.join(no_digits)

As @AshwiniChaudhary and @KirkStrauser point out, you actually do not need to use the brackets in the one-liner, making the piece inside the parentheses a generator expression (more efficient than a list comprehension). Even if this doesn't fit the requirements for your assignment, it is something you should read about eventually :) :

>>> s = '12abcd405'
>>> result = ''.join(i for i in s if not i.isdigit())
>>> result
'abcd'

Removing all numeric characters in a string, python

You can find the answer here
Removing numbers from string

From this answer:

comp_string = "xxf1,aff242342"
new_string = ''.join([i for i in comp_string if not i.isdigit()])

It creates a new string using .join from a list. That list is created using a list comprehension that iterates through characters in your original string, and excludes all digits.

How to remove all numbers from string?

For Western Arabic numbers (0-9):

$words = preg_replace('/[0-9]+/', '', $words);

For all numerals including Western Arabic (e.g. Indian):

$words = '१३३७';
$words = preg_replace('/\d+/u', '', $words);
var_dump($words); // string(0) ""
  • \d+ matches multiple numerals.
  • The modifier /u enables unicode string treatment. This modifier is important, otherwise the numerals would not match.

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 string terms in a pandas dataframe

You can apply str.replace to the Name column in combination with regular expressions:

import pandas as pd

# Example DataFrame
df = pd.DataFrame.from_dict({'Name' : ['May21', 'James', 'Adi22', 'Hello', 'Girl90'],
'Volume': [23, 12, 11, 34, 56],
'Value' : [21321, 12311, 4435, 32454, 654654]})

df['Name'] = df['Name'].str.replace('\d+', '')

print(df)

Output:

    Name   Value  Volume
0 May 21321 23
1 James 12311 12
2 Adi 4435 11
3 Hello 32454 34
4 Girl 654654 56

In the regular expression \d stands for "any digit" and + stands for "one or more".

Thus, str.replace('\d+', '') means: "Replace all occurring digits in the strings with nothing".

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, '');

How to use dart to remove all but digits from string using RegExp

You need to use

input.replaceAll(new RegExp(r"\D"), "");

See the replaceAll method signature: String replaceAll (Pattern from, String replace), from must be a Pattern class instance.

Note that r"\D", a raw string literal, is a more convenient way to define regular expressions, since regular string literals, like "\\D", require double escaping of backslashes that form regex escapes.

Strip Numbers From String in Python

Yes, you can use a regular expression for this:

import re
output = re.sub(r'\d+', '', '123hello 456world')
print output # 'hello world'

Remove numbers from string in dataframe in R

You can do this with gsub in base R :

df$ColA <- gsub('[0-9.]', '', df$ColA)
df

# ColA ColB
#1 A dff
#2 B dfa
#3 C dfd

data

df <- structure(list(ColA = c("A.12", "B.34", "C.545"), ColB = c("dff", 
"dfa", "dfd")), class = "data.frame", row.names = c(NA, -3L))


Related Topics



Leave a reply



Submit