连接组(电话验证器)

时间:2014-01-27 16:42:50

标签: regex

我需要在哪里制作电话验证器:

  • 第一个字符为+ ( or digit

  • 彼此不能有() / . -个字符

  • 仅允许digits () / . - or space

  • 长度无关紧要

实施例

123-431-123/23 - true
+312-31-32-53 - true
000-132+23 false
000()312 false
122--231 false

这是我的模式,几乎可以工作但允许字母和其他字符不在集合中:/

^[+( 0-9]{1}([0-9 .\/()-])((?:(?![.\/()-]{2}).)*$)

Regular expression visualization

Debuggex Demo

2 个答案:

答案 0 :(得分:1)

您可以尝试使用此正则表达式:

^[+(0-9](?:[0-9 ]+[()/.-])*[0-9 ]+$

regex101 demo

(?:[0-9 ]+[()/.-])*阻止你提到的特殊字符连续出现,我没有使用.通配符,因此不会匹配任何其他字符。

答案 1 :(得分:1)

这符合您的要求,并且还可以识别美国化的电话号码,例如(123) 456-7890

^(?:[+(])?(\d+)(?:\))?(?:[ -/])?(\d+)(?:[ -/])?(\d+)(?:[ -/])?(\d*)$

虽然它在debuggex中不起作用,但这是它的图像:

Regular expression visualization

它适用于regexplanet:http://fiddle.re/53ktv(按Java)

工作示例:顶级正则表达式:

   import  java.util.regex.Matcher;
   import  java.util.regex.Pattern;
/**
   <P>{@code java PhoneNumberValidatorXmpl}</P>
 **/
public class PhoneNumberValidatorXmpl  {
   public static final void main(String[] igno_red)  {

      String sOptionalSpaceDashSlash = "(?:[ -/])?";
      String sOneOrMoreDigitsCAPTURED = "(\\d+)";

      String sREPhone = "" +
         "^(?:[+(])?" +         //+ or (             [start of line, then optional]
         sOneOrMoreDigitsCAPTURED +
         "(?:\\))?" +           //Closing )          [optional]
         sOptionalSpaceDashSlash +
         sOneOrMoreDigitsCAPTURED +
         sOptionalSpaceDashSlash +
         sOneOrMoreDigitsCAPTURED +
         sOptionalSpaceDashSlash +
         "(\\d*)$";             //one-or-more digits [optional, then end of line]

         //Equivalent to:
         //String sREPhone = "" +
         //   "^(?:[+(])?" +      //+ or (             [start of line, then optional]
         //   "(\\d+)" +          //one-or-more digits CAPTURED
         //   "(?:\\))?" +        //Closing )          [optional]
         //   "(?:[ -/])?" +      //- or / or SPACE    [optional]
         //   "(\\d+)" +          //one-or-more digits CAPTURED
         //   "(?:[ -/])?" +      //- or / or SPACE    [optional]
         //   "(\\d+)" +          //one-or-more digits CAPTURED
         //   "(?:[ -/])?" +      //- or / or SPACE    [optional]
         //   "(\\d*)$";          //one-or-more digits CAPTURED [optional, then end of line]

下半部分,有测试和逻辑:

      //Create matcher with unused string, so it can be reused in test(m,s)
      Matcher m = Pattern.compile(sREPhone).matcher("");

      test(m, "123-431-123/23");  //true
      test(m, "+312-31-32-53");   //true
      test(m, "(123) 456-7890");  //true
      test(m, "000-132+23");      //false
      test(m, "000()312");        //false
      test(m, "122--231");        //false
   }
   private static final void test(Matcher m_m, String s_toTest)  {
      m_m.reset(s_toTest);
      System.out.println(s_toTest + "  -->  " + m_m.matches());
   }
}

输出:

[C:\java_code\]java PhoneNumberValidatorXmpl
123-431-123/23  -->  true
+312-31-32-53  -->  true
(123) 456-7890  -->  true
000-132+23  -->  false
000()312  -->  false
122--231  -->  false