Java Find word in a String

后端 未结 5 1272
清酒与你
清酒与你 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:10

    StringUtils.countMatches(SourceCode, "hsw.ads") ought to work, however sticking with the approach you have above (which is valid), I'd recommend a few things: 1. As John Haager mentioned, remove the opening/closing .* will help, becuase you're looking for that exact substring 2. You want to escape the '.' because you're searching for a literal '.' and not a wildcard 3. I would make this Pattern a constant and re-use it rather than re-creating it each time.

    That said, I'd still suggest using the approaches above, but I thought I'd just point out your current approach isn't conceptually flawed; just a few implementation details missing.

提交回复
热议问题