How to Remove All Special Character in a String Except Dot and Comma

Remove all the characters and numbers except comma

Possible solution is the following:

# pip install pandas

import pandas as pd
pd.set_option('display.max_colwidth', 200)

# set test data and create dataframe
data = {"text": ['100 % polyester, Paperboard (min. 30% recycled), 100% polypropylene','Polypropylene plastic', '100 % polyester, Paperboard (min. 30% recycled), 100% polypropylene', 'Bamboo, Clear nitrocellulose lacquer', 'Willow, Stain, Solid wood, Polypropylene plastic, Stainless steel, Steel, Galvanized, Steel, 100% polypropylene', 'Banana fibres, Clear lacquer', 'Polypropylene plastic (min. 20% recycled)']}
df = pd.DataFrame(data)

def cleanup(txt):
re_pattern = re.compile(r"[^a-z, ()]", re.I)
return re.sub(re_pattern, "", txt).replace(" ", " ").strip()

df['text_cleaned'] = df['text'].apply(cleanup)
df

Returns

Sample Image

how to match all special characters except a comma

This will match everything that is not numeric or not a comma or period (decimal point)

var result = str.replace(/[^0-9\.,]/g, "");

How to remove all non-alphanumeric except dot or comma between 2 digits

Try this:

str = str.replaceAll("[^a-zA-Z0-9 .,]|(?<!\\d)[.,]|[.,](?!\\d)", "");

The regex matches

  • everything you definitely don't want, or
  • a dot/comma not preceded by a digit, or
  • a dot/comma not followed by a digit

Regex to remove all special characters except periods

You can use

"""[\p{P}\p{S}&&[^.]]+""".toRegex()

The [\p{P}\p{S}&&[^.]]+ pattern matches one or more (+) punctuation proper (\p{P}) or symbol (\p{S}) chars other than dots (&&[^.], using character class subtraction).

See a Kotlin demo:

println("a-b)h.".replace("""[\p{P}\p{S}&&[^.]]+""".toRegex(), ""))
// => abh.

How to Remove Special Character Except Comma

There are a couple of ways I'd do this.

The first, quick and straight forward is to just replace all the special characters with "", using a regex and String.replaceAll

myString.replaceAll("[\\\"\\[\\]]", ""); 

(Btw, I used http://rubular.com/ as a quick way to check my regex. Remember that the regex needs to be escaped for java - I used this tool to do that.)

The alternative is that you're actually looking at the String representation of a JSON object here, so convert the JSON string into a Java array of Strings using something like org.json, and then concatenate the strings together with a , delimiter.

How to remove all non numeric characters (excluding minus, dot and comma) in a string in Javascript?

To remove all chars but digits, commas, dots and a hyphen at the start of the string, you may use

text = text.replace(/^(-)|[^0-9.,]+/g, '$1');

See the regex demo

Details

  • ^(-) - start of string and a - captured into Group 1
  • | - or
  • [^0-9.,]+ - any 1+ chars other than digits, . and ,.

The replacement is $1, i.e. if there was a leading - it will remain in the result.

A bit more comprehensive regex that only keeps the last non-final comma/dot is

text = text.replace(/^(-)|[.,](?=[^.,]*[.,](?!$))|[,.]+$|[^0-9.,]+/g, '$1');

See this regex demo

Here, some more alternatives are added:

  • [.,](?=[^.,]*[.,](?!$)) - matches . or , that are followed with another . or , somewhere after 0+ chars other than . and ,
  • [,.]+$ - matches any 1+ trailing commas/dots.

How to remove any special characters from a string even with dot and comma and spaces

If what you said in title:

remove any special characters from a string even with dot and comma and spaces

means that you'd want to keep only digits and letters, then such a regular expression might do:

SQL> with test (col) as
2 (select 'HI every one. I want to (2-21-2022) remove the comma-dot and other any special character from string(123)' from dual)
3 select regexp_replace(col, '[^[:alnum:]]') result
4 from test;

RESULT
---------------------------------------------------------------------------------
HIeveryoneIwantto2212022removethecommadotandotheranyspecialcharacterfromstring123


SQL>

On the other hand, that's not what example you posted represents (as already commented).

Remove all special characters except space from a string using JavaScript

You should use the string replace function, with a single regex.
Assuming by special characters, you mean anything that's not letter, here is a solution: