查找单词时的正则表达式问题

时间:2014-08-21 05:50:18

标签: java regex

如何使用正则表达式?

1 个答案:

答案 0 :(得分:2)

我不会给出替换逻辑(过多的勺子喂食),但我可以告诉你如何找到一个单词以z开头。

   public static void main(String args[]){
   String s = "abc zdefg hijk asdsaz";
         Pattern p = Pattern.compile("\\b(?!z)(\\w+)\\b");
  // \\b is word boundary matcher.
  // ?! negative lookahead and checks if a word doesn't start with z
  // //w+ matches one or more characters only if previuos condition holds true.
         Matcher m = p.matcher(s);
         while(m.find()){
             System.out.println(m.group());
         }
    }

O / P:

abc
hijk
asdsaz