当前位置:编程学习 > JAVA >>

【高分求解】帮忙看看这个正则表达式

String currentLine = "namespace POA_circuitCutMgr";
if(currentLine.matches("\\bnamespace[ ]POA_.*")
{
     System.out.println("ok");
}

问题1:我只是想匹配以namespace POA_开头的任意字符串, 这里为什么匹配不成功? 我用正则表达式工具,是可以匹配成功的。 --------------------编程问答-------------------- 为啥不使用String的startsWith("namespace POA_"); --------------------编程问答--------------------

public class TestRegular 
{
public static void main(String[] args)
{
String currentLine = "namespace POA_circuitCutMgr";
if(currentLine.matches("\\bnamespace[ ]POA_.*"))//这加个")"
{
System.out.println("ok");
}
}
}


运行结果:
ok
--------------------编程问答-------------------- 楼主的代码能编译通过??? --------------------编程问答-------------------- --------------------编程问答-------------------- String currentLine = "namespace POA_circuitCutMgr";
if(currentLine.matches("\\bnamespace[ ]POA_.*"));
{
System.out.println("ok");
}
看红色的部分。 --------------------编程问答--------------------
引用 5 楼 hudie1234567 的回复:
String currentLine = "namespace POA_circuitCutMgr";
if(currentLine.matches("\\bnamespace[ ]POA_.*"));
{
System.out.println("ok");
}
看红色的部分。

错了,后面多了个分号。 --------------------编程问答--------------------
引用 1 楼 oh_Maxy 的回复:
为啥不使用String的startsWith("namespace POA_");


多简单,呵呵
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,