2017-06-02 57 views
1

在我的代码我验证手机号与正则表达式模式。如果我硬编码正则表达式模式在我的代码其匹配和工作方式expected.But如果我从属性中获取模式文件它不是matching.Below是我的代码RegEx模式不匹配,如果从属性文件加载模式

public class RegularExpTest { 
    public static final Hashtable<String, String> configDetails = new Hashtable<String, String>(); 
    public static void main(String[] args) { 
     try { 

      String str = "+917777777777"; 

      Properties properties = new Properties(); 
      InputStream input = new FileInputStream(new File(System 
        .getProperty("conf.path") 
        + "/webconfiguration.xml")); 
      properties.loadFromXML(input); 
      if (properties != null) { 
       Enumeration<Object> keyString = properties.keys(); 
       String key = ""; 
       while (keyString.hasMoreElements()) { 
        key = keyString.nextElement().toString(); 

        configDetails.put(key, properties.getProperty(key)); 
       } 
      } 
      String mobPattern = configDetails.get("MOB.PATTERN"); 
      Pattern mobilePattern = Pattern.compile(mobPattern); 
      if(mobilePattern.matcher(str).matches()) { 
       System.out.println("true"); 
      } else { 
       System.out.println("false"); 
      } 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 

    } 
} 

输出

True if I hard code the pattern in code 
False if I fetch the pattern from property file 

下面是我的正则表达式

^(\\+91)?[789]\\d{9}$ 

为什么它不工作如果我从属性文件中获取模式。 webconfiguration.xml

<entry key="MOB.PATTERN">^(\\+91)?[789]\\d{9}$</entry> 

任何帮助将不胜感激!!!!

+1

你是否在文件中转义反斜杠? – shmosel

+0

缺少最重要的信息:属性文件中写入了什么?如果您打印“mobPattern”的内容,也可以提供帮助。 –

+0

^(\\ + 91)?[789] \\ d {9} $这是我给出的文件格式 – Madhesh

回答

1

删除文件中的反斜杠。现在按预期工作。谢谢@shmosel。