Skip to content

Commit a06e491

Browse files
committed
feat: Remove special characters from a string using regex
- Input: String containing mixed characters including letters, digits, and special symbols (e.g., "a!B@c#1$2%3") - Applied regex pattern `[^a-zA-Z0-9]` with `replaceAll()` method to remove all characters *except*: - Lowercase letters (a–z) - Uppercase letters (A–Z) - Digits (0–9) - Replaced disallowed characters with a space to retain readability. 🔍 Explanation: - `[^a-zA-Z0-9]` is a negated character class, meaning it matches any character *not* in the allowed ranges. - Useful for sanitizing user input, cleaning noisy strings, or preparing data for processing. 🧪 Example: - Input: `a!B@c#1$2%3` - Output: `a B c 1 2 3` Signed-off-by: Somesh diwan <[email protected]>
1 parent 2b94911 commit a06e491

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
class RegularExpressionChallange3 {
2+
public static void main(String[] args) {
3+
String str = "a!B@c#1$2%3";
4+
5+
System.out.println(str.replaceAll("[^a-zA-Z0-9]", " "));
6+
}
7+
}
8+
19
/*
210
Remove Special Characters from a String
311
@@ -6,13 +14,3 @@
614
method called str.replace() and str.replaceAll()
715
what is allowed is [^a-zA-Z0-9] use symbol not ^ if not among this.
816
*/
9-
10-
class RegularExpressionChallange3
11-
{
12-
public static void main(String[] args)
13-
{
14-
String str = "a!B@c#1$2%3";
15-
16-
System.out.println(str.replaceAll("[^a-zA-Z0-9]", " "));
17-
}
18-
}

0 commit comments

Comments
 (0)