Java Find word in a String

后端 未结 5 1274
清酒与你
清酒与你 2021-01-15 10:26

I need to find a word in a HTML source code. Also I need to count occurrence. I am trying to use regular expression. But it says 0 match found.

I am using regular ex

5条回答
  •  天涯浪人
    2021-01-15 11:20

    Your code and regular expression is valid. You don't need to include the .* at the beginning and the end of your regex. For example:

    String t = "hsw.ads hsw.ads hsw.ads";
    int count = 0;
    Matcher m  = Pattern.compile("hsw\\.ads").matcher(t);
    while (m.find()){ count++; }
    

    In this case, count is 3. And another thing, if you're going to use a regex, if you REALLY want to specifically look for a '.' period between hsw and ads, you need to escape it.

提交回复
热议问题