测试正则表达式
原创
©著作权归作者所有:来自51CTO博客作者DonaldY的原创作品,请联系作者获取转载授权,否则将追究法律责任
用于验证正则表达式是否具备你所期待的匹配功能
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Donald on 2016/12/6.
*/
/**
* Allows you to easily try out regular expressions.
*/
public class TestRegularExpression
public static void main(String []args){
if(args.length < 2){
System.out.println("Usage: \njava TestRegularExpression " +
"characterSequence regularExpression+");
System.exit(0);
}
System.out.println("Input: \"" + args[0] + "\"");
for (String arg : args){
System.out.println("Regular expression: \"" + arg + "\"");
Pattern p = Pattern.compile(arg);
Matcher m = p.matcher(args[0]);
while(m.find()){
System.out.println("Math \"" + m.group() + "\" at positions " +
m.start() + "-" + (m.end() - 1));
}
}
}
}
利用args输入 {Args: abcabcabcdefabc “abc+” “(abc)+” “(abc){2,}”}
下面是IDEA的设置
结果图