Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>split</code> to count isn't the most efficient, but if you insist on doing that, the proper way is this:</p> <pre><code>haystack.split(needle, -1).length -1 </code></pre> <p>If you don't set <code>limit</code> to <code>-1</code>, <code>split</code> defaults to <code>0</code>, which removes trailing empty strings, which messes up your count.</p> <p>From <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29" rel="noreferrer">the API</a>:</p> <blockquote> <p>The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. [...] If <code>n</code> is zero then [...] trailing empty strings will be discarded.</p> </blockquote> <p>You also need to subtract 1 from the <code>length</code> of the array, because <code>N</code> occurrences of the delimiter splits the string into <code>N+1</code> parts.</p> <hr> <p>As for the regex itself (i.e. the <code>needle</code>), you can use <code>\b</code> the word boundary anchors around the <code>word</code>. If you allow <code>word</code> to contain metacharacters (e.g. count occurrences of <code>"$US"</code>), you may want to <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#quote%28java.lang.String%29" rel="noreferrer"><code>Pattern.quote</code></a> it.</p> <hr> <blockquote> <p>I've come up with this:</p> <pre><code>numThe += line.split("[^a-zA-Z][Tt]he[^a-zA-Z]", -1).length - 1; </code></pre> <p>Though still getting some strange numbers. I was able to get an accurate general count (without the regular expression), now my issue is with the regexp. </p> </blockquote> <p>Now the issue is that you're not counting <code>[Tt]he</code> that appears as the first or last word, because the regex says that it has to be preceded/followed by some character, something that matches <code>[^a-zA-Z]</code> (that is, your match must be of length 5!). You're not allowing the case where there <em>isn't</em> a character at all!</p> <p>You can try something like this instead:</p> <pre><code>"(^|[^a-zA-Z])[Tt]he([^a-zA-Z]|$)" </code></pre> <p>This isn't the most concise solution, but it works.</p> <p>Something like this (using <a href="http://www.regular-expressions.info/lookaround.html" rel="noreferrer">negative lookarounds</a>) also works:</p> <pre><code>"(?&lt;![a-zA-Z])[Tt]he(?![^a-zA-Z])" </code></pre> <p>This has the benefit of matching <em>just</em> <code>[Tt]he</code>, without any extra characters around it like your previous solution did. This is relevant in case you actually want to process the tokens returned by <code>split</code>, because the delimiter in this case isn't "stealing" anything from the tokens.</p> <hr> <h3>Non-<code>split</code></h3> <p>Though using <code>split</code> to count is rather convenient, it isn't the most efficient (e.g. it's doing all kinds of work to return those strings that you discard). The fact that as you said you're counting line-by-line means that the pattern would also have to be recompiled and thrown away every line.</p> <p>A more efficient way would be to use the same regex you did before and do the usual <code>Pattern.compile</code> and <code>while (matcher.find()) count++;</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload