My Java regular expression can't find a simple string literal but String.indexOf indicates it's present : <p>I have some SQL that starts as follows: </p>

<pre><code>String sql = "SELECT "+
        "    SI.SITE_ID "; ....
</code></pre>

<p><em>Eventually</em> I want to write a regular expression which, based on the literal string (column name) "SITE_ID", will find the <em>fully qualified</em> column name (with the "SI." on front).  After writing what I thought would work for that purpose (<code>Pattern.compile("\\s+\\w+\\." + "SITE_ID" + "\\s+")</code> and then, eventually, extract a capture) but it not returning the result I expected, I decided to simplify.</p>

<p>Now though even though I have simplified as much as I can possibly think to do, simply to search for the string literal "SITE_ID" in the <code>sql</code> variable, it still returns false, but <code>sql.indexOf()</code> returns a value greater than -1, so <code>sql</code> <em>does</em> contain the string:</p>

<pre><code>boolean foundSiteId = Pattern.compile("SITE_ID").matcher(sql).matches(); // false
int siteIdPos = sql.indexOf("SITE_ID"); // 12
</code></pre>

<p>I find this surprising; it's not as though I'm trying to anchor "SITE_ID" to the front with <code>^</code> or the end with <code>$</code>.  Additionally I have gone out to <a href="https://www.freeformatter.com/java-regex-tester.html" rel="nofollow noreferrer">https://www.freeformatter.com/java-regex-tester.html</a> (because re-compiling code over and over is time consuming) to try, and if I enter both "SITE_ID" (without the quotes) as the "Java Regular Expression" and "Entry to test against" it does return true.  However if I provide " SITE_ID " with a leading and trailing space to test against, it returns true.</p>

<p>I guess I must just have some fundamental mis-understanding of Java regular expressions, though I am reasonably versed in them from other languages.  What am I doing wrong, thanks.</p>
