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

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...