2014-10-30 129 views
0

我希望有一个模式匹配,在它(字母,数字,特殊charactres)拥有一切字符串Rexexp以匹配字符串

公共静态无效的主要(字符串[所有的数字,字母,特殊字符]参数){

String retVal=null; 

    try 
    { 
     String s1 = "[0-9a-zA-Z].*:[0-9a-zA-Z].*:(.*):[0-9a-zA-Z].*"; 
     String s2 = "BNTPSDAE31G:BNTPSDAE:Healthcheck:Major"; 

     Pattern pattern = null; 


     //if (! StringUtils.isEmpty(s1)) 
     if ((s1 != null) && (! s1.matches("\\s*"))) 
     { 
       pattern = Pattern.compile(s1); 
     } 

     //if (! StringUtils.isEmpty(s2)) 
     if (s2 != null) 
     { 
      Matcher matcher = pattern.matcher(s2); 
      if (matcher.matches()) 
      { 
       retVal = matcher.group(1); 

       // A special case/kludge for Asentria. Temp alarms contain "Normal/High" etc. 
       // Switch Normal to return CLEAR. The default for this usage will be RAISE. 
       // Need to handle switches in XML. This won't work if anyone puts "normal" in their event alias. 
       if ("Restore".equalsIgnoreCase (retVal)) 
       { 

       } 

      } 
     } 
    } 

    catch(Exception e) 
    { 
     System.out.println("Error evaluating args : "); 
    } 
    System.out.println("retVal------"+retVal); 

}

和输出是: 健康检查 荷拉使用该[0-9A-ZA-Z] *我匹配仅alpahbets和数字,但我想匹配。字符串如果有特殊字符,也

任何帮助,高度赞赏

+0

什么是不允许的?我对这个问题有点困惑。你能帮我理解一下吗? – 2014-10-30 03:12:08

回答

0

试试下面的正则表达式,它为我工作:)

[^:]+ 

你可能需要把它全球改性得到它以匹配所有字符串。如果你想匹配单个元素

试试这个:

0

试试这个

2.1.2 :001 > s = "[email protected]#:$%adasd1213" 
2.1.2 :008 > s.scan(/./) 
=> ["a", "s", "a", "d", "3", "4", "3", "5", "@", "#", ":", "$", "%", "a", "d", "a", "s", "d", "1", "2", "1", "3"] 

,或者你想匹配所有一次尝试这个办法:

2.1.2 :009 > s.scan(/[^.]+/) 
=> ["[email protected]#:$%adasd1213"]