1 Reply Latest reply: Nov 29, 2013 2:33 AM by gimbal2 RSS

    regular expression in java

    eb20c2e9-ade7-43c7-8a85-f40499c1a389

      i need a regular expression in java for phone number which that does not allow any character special character only numbers should be allowed..

      my sample program is

       

      import java.util.regex.Matcher;

      import java.util.regex.Pattern;

       

       

      public class c {

          

        public String removeOrReplacePhoneNumber(String input) {

        if (null == input)

        return input;

        Pattern pt = Pattern.compile("[^0-9]");

        Matcher match = pt.matcher(input);

       

       

        while (match.find()) {

        String s = match.group();

        if ((int) s.charAt(0) == 188) {

        input = input.replaceAll("\\" + s, "L");

        } else if ((int) s.charAt(0) == 249) {

        input = input.replaceAll("\\" + s, "&");

        } else {

        input = input.replaceAll("[^a-zA-Z0-9\\s]" + s, "");

        }

        }

        return input;

        }

          public static void main(String[] args) {

              c obj= new c();

              String l=obj.removeOrReplacePhoneNumber("+042769%56-397");

              System.out.println(l);

            

          }

       

      }

       

       

      but this is allowing characters like a,b,c..