Replace Non Alphabetic Characters
This program removes non alphabetic characters from string using regex.
Problem Statement
Write a Java program to remove non alphabetic characters from "Eduinq123@Test!".
Source Code
| 1 | public class RegexReplaceNonAlpha { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Regex Replace Non Alpha"); |
| 4 | String str="Eduinq123@Test!"; |
| 5 | String result=str.replaceAll("[^A-Za-z]",""); |
| 6 | System.out.println("After removal: "+result); |
| 7 | } |
| 8 | } |
Program Output
Eduinq Regex Replace Non Alpha After removal: EduinqTest
Explanation
The character class [^A-Za-z] matches any character that is not a letter, and replaceAll() removes each such character, demonstrating regex-based string cleaning.