字符串strConfig,形如eric;cole;bean
字符串strName为eric
如何判断strName是否在strConfig中注:只有全词匹配才行,例如当strName为ic或ol时,要匹配失败
字符串strName为eric
如何判断strName是否在strConfig中注:只有全词匹配才行,例如当strName为ic或ol时,要匹配失败
解决方案 »
- Struts2 数组传递方法
- iso-8859-1、utf-8、gb2312 和gbk的区别
- 麻烦高手给解决一下
- xtree2 可否展开节点加载XML (一次性加载所有节点速度太慢了,如何在展开某个节点时,加载XML增加子节点,如何写xtree的展开事件?)
- tomcat项目启动问题,如何找到对应的应用启动类
- 盼高人指点。。。。。。。。。。。。
- jsp文件中嵌入javascript语句产生错误??
- Jbuilder9中使用Context连接Oracle数据库的问题:总提示Context path is already in use
- 关于weblogic的中文问题,高分求教!
- 新年处女贴-祝JSP里面的朋友新年快乐.散分~~~~~~~
- JS中能不能调用struts2的Action,并向Action中传参数?
- 可输入的下拉框,并能根据数据输入内容过滤
Pattern p = Patter.compile("eric");
Matcher m = p .....m.match(strConfig);
String strConfig="eric;cole;bean";
String strName = "eric";
Pattern p = Pattern.compile(strName);
Matcher m = p.matcher(strConfig);
if(m.find()){
System.out.println(m.group());
}
strConfig = eric;cole;gole
匹配eric结果:true
匹配ic结果:true
匹配e结果:true
匹配ole结果:true请对自己给出的答案负责好么
这个怎么用哇
strConfig.matches(regex) 返回的是false
是不是我用的方法错了
String strConfig="eric;cole;bean";
Pattern p =Pattern.compile(regex);
Matcher m=p.matcher(strConfig);
while(m.find()){
String s = m.group(0);
System.out.println(s);
}
String strConfig="eric;cole;bean";
String strName = "eric";
Pattern p = Pattern.compile(strName+";");
Matcher m = p.matcher(strConfig);
if(m.find()){
System.out.println("true");
}else{
System.out.println("false");
}
String strConfig="eric;cole;bean";
String strName = "eric"; String[] aa = strConfig.split(";");
for(String s : aa){
if(strName.equals(s)){
System.out.println("true");
}
}
或者 这个比较简单,不需要用到正则,如:if(strConfig.indexOf(strName+";")!=-1){
true;}
哦抱歉,弄错了一个符号,$换成^就可以了 String strConfig = "eric;cole;bean";
String strName="eric";
String regl = "^" + strName + ";";
Matcher m = Pattern.compile(regl).matcher(strConfig);
System.out.println(m.find());
多谢 可以用了~~~请问为什么用Pattern.matches(regex, strConfig)和strConfig.matches(regex) 就会返回一个false
估计你没试过strName = "bean";
因为matches()是整体匹配,即strConfig整个字符串完全匹配这个正则表达式
而find()是包含匹配,即只要在strConfig中发现有部分匹配就可以了
好像你说的很对
api中 说
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
可以被
boolean b = Pattern.matches("a*b", "aaaaab");
代替
于是我想当然的就任认为所有情况下 都能代替
文学功底不够
String s = "eric;cole;bean";
String s1 = "cole"; //s1 = co
if (Arrays.binarySearch(s.split(";"), s1) >= 0) {
System.out.println("yes");
}
if (Arrays.asList(s.split(";")).contains(s1)) {
System.out.println("yes");
}
这个正则有问题的,只能匹配 eric,其他的,如 cole 匹配不了
那么
String s = "eric;cole;bean";
String s1 = "eric";
if (s.startsWith(s1+";")) 就可以了