我想要一个模式匹配包含所有内容的字符串(字母,数字,特殊字符)
public static void main(String [] args){
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);
} 的
输出是: 健康检查 Hera使用这个[0-9a-zA-Z]。*我只匹配alpahbets和数字,但我想匹配字符串,如果它也有特殊字符
非常感谢任何帮助
答案 0 :(得分:0)
尝试以下正则表达式,它适用于我:)
[^:]+
您可能需要在其上放置一个全局修饰符,以使其与所有字符串匹配。
答案 1 :(得分:0)
试试这个:
如果您想匹配单个元素,请尝试以下方法:
2.1.2 :001 > s = "asad3435@#:$%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(/[^.]+/)
=> ["asad3435@#:$%adasd1213"]