Replacing all non-alphanumeric characters with empty strings
Use [^A-Za-z0-9]
.
Note: removed the space since that is not typically considered alphanumeric.
Replace all non alphanumeric characters, new lines, and multiple white space with one space
Be aware, that \W
leaves the underscore. A short equivalent for [^a-zA-Z0-9]
would be [\W_]
text.replace(/[\W_]+/g," ");
\W
is the negation of shorthand \w
for [A-Za-z0-9_]
word characters (including the underscore)
Example at regex101.com
How do I remove all non alphanumeric characters from a string except dash?
Replace [^a-zA-Z0-9 -]
with an empty string.
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");
Replace all non-alphanumeric characters in a string
Regex to the rescue!
import re
s = re.sub('[^0-9a-zA-Z]+', '*', s)
Example:
>>> re.sub('[^0-9a-zA-Z]+', '*', 'h^&ell`.,|o w]{+orld')
'h*ell*o*w*orld'
Regular Expression to replace all non alphanumeric characters except / to empty() character
Use regex [^a-zA-Z0-9//]
insted of [^a-zA-Z0-9]
to escape /
LibString.replaceAll(token, "[^a-zA-Z0-9//]", "");
Replacing all non-alphanumeric characters except some characters
Use explicit white list instead:
replaceAll("[^a-zA-Z0-9ÆØÅæøå]","_")
Look at the similar question
Replace all Non-Alphanumeric Characters except one particular pattern using RegEx in Python
You can use re.sub and a pattern with a capture group
(\d+(?:\.\d+))|\W+
The pattern matches:
(\d+(?:\.\d+))
Capture digits with an optional decimal part with a dot in group 1|
OR\W+
Match 1+ non word characters to replace with a single space (or use a negated character class[^a-zA-Z0-9]+
to keep matching an underscore)
In the replacement keep the capture group, and replace the match for 1+ non word characters with a space.
See a regex demo and a Python demo
import re
s = "xyz - aabc 123.56 cancer s15.2 date 12/03/2021 @ dd hospital www.someurl ocr.5rror 123.sometext"
pattern = r"(\d+(?:\.\d+))|\W+"
print(re.sub(pattern, lambda x: x.group(1) if x.group(1) else " ", s))
Output
xyz aabc 123.56 cancer s15.2 date 12 03 2021 dd hospital www someurl ocr 5rror 123 sometext
replace non-alphamumeric and multiple spaces
To achieve this in a single regular expression, you can alternate with capturing the space in a group, and then replace with that group (which will be the empty string if the other alternation was used):
const replace = str => str.replace(/( )+|[^\da-z ]+/gi, '$1');console.log(replace('foobar'));console.log(replace('foo bar'));console.log(replace('foo###bar'));
Related Topics
How to Set Dpi Information in an Image
Do Hibernate Table Classes Need to Be Serializable
Easy Way of Running the Same Junit Test Over and Over
Try-Catch-Finally-Return Clarification
Replace All Occurrences of a String Using Stringbuilder
Spring Autowiring Using @Configurable
Why Doesn't Java Allow Generic Subclasses of Throwable
How to Make a Java Class That Implements One Interface with Two Generic Types
How to Find Out the Currently Logged-In User in Spring Boot
Shared Memory Between Two Jvms
Java Enum and Additional Class Files
Why Is an Instance Variable of the Superclass Not Overridden by a Subclass
Is There an Equivalent of Java.Util.Regex for "Glob" Type Patterns
How to Add a New Line of Text to an Existing File in Java
Java; String Replace (Using Regular Expressions)
Java - How to Load Different Versions of the Same Class
Error: Servlet Jar Not Loaded... Offending Class: Javax/Servlet/Servlet.Class