2

I am interesting in finding the number of times a word appears in a String.
I have seen the example of SUN Matcher Demo using the matcher (constructs a regex and counts the groups).

I was interested if this is the only way or there is something else like e.g.

Regex.Matches( input, "true" ).Count in C# (from SO question).

Or in my context:

If I am in a loop using contains to check if certain words in the List appear in the String is there a simple/elegant way to get (on the spot??) the number of times the word occurs (that I already know it exists)?

Community
  • 1
  • 1
Cratylus
  • 52,998
  • 69
  • 209
  • 339

1 Answers1

3

You can do something like

Pattern p = Pattern.compile(toFind, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
int occurances = p.split(text, -1).length -1;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • he he funny, i almost typed the same answer and deleted it after seeing yours – Suraj Chandran Jan 23 '12 at 16:57
  • +1.I like this.If I want case insensitive search I must do `"(?i)"+toFind` right?So should I follow your suggestion as `int occurances = text.split(new StringBuilder("(?i)").append(toFind).toString());` because I am in a loop? – Cratylus Jan 23 '12 at 17:05
  • try something like `text.toLowerCase().split(...)` for a lowercase search in the source, asserting the find pattern is already in lowercase. – Ortwin Angermeier Jan 23 '12 at 17:10
  • @ortag:How is this better?Doesn't `toLower` create a new `String`? – Cratylus Jan 23 '12 at 17:31
  • If you are in a loop, you can use a compiled pattern. Creating the StringBuilder yourself won't improve efficiency. – Peter Lawrey Jan 23 '12 at 18:52
  • @Peter Lawrey:But how can I do it, if I know what to append to create the pattern only when I am inside the loop? – Cratylus Jan 23 '12 at 18:59
  • In that case you need to use it inside the loop and you cannot optimise it that way. One advantage with using Pattern is that you can make it LITERAL to avoid having to quote special characters. – Peter Lawrey Jan 23 '12 at 19:08
  • You mean do `Pattern p = Pattern.compile("(?i)"+toFind));`.How is this better?Sorry I lost you :( – Cratylus Jan 23 '12 at 19:37
  • I mean `Pattern p = Pattern.compile(toFind, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);` – Peter Lawrey Jan 23 '12 at 20:02
  • @Peter Lawrey:Thank you for your help! – Cratylus Jan 23 '12 at 20:18
  • @Peter Lawrey:This does not word if the work is at the end of the String – Cratylus Jan 23 '12 at 21:39