Friday, October 12, 2018

How do you remove the special characters from a string using Regex in java ?

Problem statement:
can you remove the special character from a string using regex in java?

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RemoveSpecialCharacter{
      public static void main(String args[]){
      String c = "p$82-a/*@#^&873";
     
      Pattern pt = Pattern.compile("^a-zA-Z0-9");
      Matcher match = pt.matcher(c);
      while(match.find()){
        String s = match.group();
        c = c.replaceAll("\\"+s, "");
       }
        System.out.println(c);
      }
}
Output:
p82a873

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...