Use [^A-Za-z0-9].

Note: removed the space since that is not typically considered alphanumeric.

Answer from Mirek Pluta on Stack Overflow
Discussions

Trying to remove non-alphabetical characters from inputted string need help pls
The problem is that you're modifying the string while you're iterating over it. For example, if you have the input string 123, then on the first iteration i=0, so you'll remove the 1, resulting in the string 23. On the second iteration, i=1, and you'll remove the second character of the string which is 3. But you skipped the 2 because it was shifted backwards to an earlier index. You can hack around this, but it's simpler and more efficient to copy the input to a different variable and skip the non-alphabetical characters, instead of removing them in-place. More on reddit.com
🌐 r/learnprogramming
8
0
February 22, 2024
How to remove non-alphanumeric characters in Java?
What are the best methods to remove non-alphanumeric characters from a string in Java? -- If you haven't seen the new Community Initiative, click the link to learn more about EddiEE! ... We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads. ... you may start from the below code on removing all ... More on experts-exchange.com
🌐 experts-exchange.com
September 16, 2022
removing all non-letter characters from a string? ((using regex))
This is very simple with regex. You just need to replace non-words (/\W/ig). So: var string = "lakjsdlkasjdlsaj@£$%^&*klajdlaskjds"; string.replace(/\W/ig, ""); --> "lakjsdlkasjdlsajklajdlaskjds" \w == words, \W == non words. You don't need a loop here as we're using /g which stands for global, so we replace every instance. And just incase I'm using /i as well which ignores the case. As it's regex you can just do /ig and the selectors will stack to ignore case and global. My favorite regex visualiser lives here: https://jex.im/regulex/#!embed=false&flags=ig&re=%5CW More on reddit.com
🌐 r/learnjavascript
10
4
September 28, 2015
How to erase all non alphanumeric characters from a string?
I want to erase all non alphanumeric characters from a string. I've tried the following way using erase() function. But it erases all the characters from where it finds a non alphanumeric character. If I input: it. is? awesome! More on cplusplus.com
🌐 cplusplus.com
🌐
Baeldung
baeldung.com › home › java › java array › removing all non-alphabetic characters from string array in java
Removing All Non-alphabetic Characters From String Array in Java | Baeldung
November 18, 2024 - In this tutorial, we’ll look at some of the most effective and straightforward methods to remove non-alphabetic characters from strings in an array. We’ll discuss techniques using regular expressions, the Stream API, StringBuilder, and libraries like Apache Commons Lang. Each method will be illustrated with simple code examples and tests. Let’s start with regular expressions to remove all non-alphabetic characters from the String array.
🌐
GeeksforGeeks
geeksforgeeks.org › java › remove-all-non-alphabetical-characters-of-a-string-in-java
Remove all non-alphabetical characters of a String in Java - GeeksforGeeks
March 24, 2023 - In this approach, we use the replaceAll() method to replace all non-alphabetic characters with an empty string. The regular expression "[^a-zA-Z]" matches any character that is not an English alphabetical letter (both uppercase and lowercase).
🌐
Reddit
reddit.com › r/learnprogramming › trying to remove non-alphabetical characters from inputted string need help pls
r/learnprogramming on Reddit: Trying to remove non-alphabetical characters from inputted string need help pls
February 22, 2024 -

Hey guys! For class I'm trying to make a program to remove all non-alpha characters from a inputted string. Can you tell me why this isn't working?

For some reason when I input for example "testing test ! bla -o-" it outputs "testingtest!bla-o"

#include <iostream>
using namespace std;
int main() {

string inputStr;
getline(cin, inputStr);
for (int i = 0; i < inputStr.size(); i++){
if (!isalpha(inputStr[i])){
inputStr.replace(i, 1, "");
}
}
cout << inputStr;
return 0;
}

Find elsewhere
🌐
Experts Exchange
experts-exchange.com › questions › 29246681 › How-to-remove-non-alphanumeric-characters-in-Java.html
Solved: How to remove non-alphanumeric characters in Java? | Experts Exchange
September 16, 2022 - https://www.geeksforgeeks.org/remove-all-occurrences-of-a-character-in-a-string/ then replace the below line ... Select allOpen in new window of course, the variable "c" here should be a string instead of char. ... Here is one way. import java.util.Scanner; public class Remover { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.println("Please type a string."); String input = scanner.nextLine(); System.out.println(input.replaceAll("[^a-zA-Z0-9]", "")); } }
🌐
Brainly
brainly.com › computers and technology › high school › how can we ignore all non-alphanumeric characters in java?
[FREE] How can we ignore all non-alphanumeric characters in Java? - brainly.com
In Java, you can ignore all non-alphanumeric characters by using the String.replaceAll() method with the regex pattern [^a-zA-Z0-9]. This pattern matches any character that is not a letter or a number and replaces it with an empty string, ...
🌐
w3resource
w3resource.com › java-exercises › re › java-re-exercise-21.php
Java - Remove all non-alphanumeric characters from a string
March 16, 2026 - public class test { public static void main(String[] args) { String text ="Java Exercises"; System.out.println("Original string: "+text); System.out.println("After removing all non-alphanumeric characters from the said string: "+validate(text)); text ="Ex@^%&%(ercis*&)&es"; System.out.println("\nOriginal string: "+text); System.out.println("After removing all non-alphanumeric characters from the said string: "+validate(text)); } public static String validate(String text) { return text.replaceAll("(?i)[^A-Z]", ""); } }
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › remove-non-alphanumeric-characters-java-string
Java alphanumeric patterns: How to remove non-alphanumeric characters from a Java String | alvinalexander.com
April 18, 2019 - Java String "alphanumeric" tip: How to remove non-alphanumeric characters from a Java String. Here's a sample Java program that shows how you can remove all characters from a Java String other than the alphanumeric characters (i.e., a-Z and 0-9).
🌐
Reddit
reddit.com › r/learnjavascript › removing all non-letter characters from a string? ((using regex))
r/learnjavascript on Reddit: removing all non-letter characters from a string? ((using regex))
September 28, 2015 -

I am currently trying this two different ways and they aren't working - I'm checking the input value and running this every time a key is pressed:

input.replace(/[^a-zA-z]/, "");

and

for(var i = 0; i < input.length; i++){
	   if( input[i] ==  /[^a-zA-z]/g){
	   	console.log("input " + input[i] + " is not a letter!");
	    	input.replace(input[i], "");
	   }
	}

jsfiddle here

🌐
LeetCode
leetcode.com › problems › first-unique-character-in-a-string
First Unique Character in a String - LeetCode
Can you solve this real interview question? First Unique Character in a String - Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. Example 1: Input: s = "leetcode" Output: 0 Explanation: The character 'l' at index 0 is the ...
🌐
OWASP Foundation
owasp.org › www-community › attacks › xss
Cross Site Scripting (XSS) | OWASP Foundation
The code in this example operates correctly if eid contains only standard alphanumeric text. If eid has a value that includes meta-characters or source code, then the code will be executed by the web browser as it displays the HTTP response. Initially, this might not appear to be much of a vulnerability. After all, why would someone enter a URL that causes malicious code to run on their own computer?
🌐
gosamples
gosamples.dev › tutorials › remove non alphanumeric characters from a string in go
🧽 Remove non-alphanumeric characters from a string in Go
May 19, 2022 - package main import ( "fmt" "regexp" ) var nonAlphanumericRegex = regexp.MustCompile(`[^\p{L}\p{N} ]+`) func clearString(str string) string { return nonAlphanumericRegex.ReplaceAllString(str, "") } func main() { str := "Test@%String#321gosamples.dev ـا ą ٦" fmt.Println(clearString(str)) }
🌐
Interview Kickstart
interviewkickstart.com › home › blogs › learn › how to remove all non-alphanumeric characters from a string in java?
Remove Non-Alphanumeric Characters from a String in Java
December 19, 2024 - So, we use this method to replace the non-alphanumeric characters with an empty string. Our regular expression will be: [^a-zA-Z0-9]. This means, “only consider pattern substring with characters ranging from ‘a’ to ‘z’, ‘A’ to ‘Z’ and ‘0’ to ‘9’.” ... Here ^ Matches the beginning of the input: It means, “replace all substrings with pattern [^a-zA-Z0-9] with the empty string.”
🌐
Cplusplus
cplusplus.com › forum › beginner › 148876
How to erase all non alphanumeric characters from a string?
I want to erase all non alphanumeric characters from a string. I've tried the following way using erase() function. But it erases all the characters from where it finds a non alphanumeric character. If I input: it. is? awesome!
🌐
W3Schools
w3schools.com › htmL › html_entities.asp
HTML Character Entities
If you write 10 spaces in your text, the browser will remove 9 of them. To add real spaces to your text, you can use the &nbsp; character entity. The non-breaking hyphen (&#8209;) is used to define a hyphen character (‑) that does not break into a new line. Entity names are case sensitive. A diacritical mark is a "glyph" added to a letter. Some diacritical marks, like grave ( ̀) and acute ( ́) are called accents. Diacritical marks can be used in combination with alphanumeric characters to produce a character that is not present in the character set (encoding) used in the page.
🌐
EE-Vibes
eevibes.com › computing › how-to-remove-all-non-alphanumeric-characters-from-a-string-in-java
How to remove all non-alphanumeric characters from a string in Java - EE-Vibes
December 9, 2021 - Call re.sub(pattern, repl, string) with pattern as "[^0-9a-zA-Z]+" to replace every non-alphanumeric character with repl in string. ... Given a string str, the undertaking is to eliminate all non-alphanumeric characters from it and print the changed it.
🌐
Coderanch
coderanch.com › t › 411171 › java › Removing-Alpha-Characters-String
Removing Alpha Characters from a String (Beginning Java forum at Coderanch)
July 21, 2008 - That first argument is supposed to be a regular expression. A well-formed regular expression representing a sequence of one or more alphabetic characters would be "\{L}+" (this matches all Unicode letters.) With the proper escaping, this looks like pnstring = pnstring.replaceAll("\\{L}+","");