I have some SQL that starts as follows:
String sql = "SELECT "+
" SI.SITE_ID "; ....
Eventually I want to write a regular expression which, based on the literal string (column name) "SITE_ID", will find the fully qualified column name (with the "SI." on front). After writing what I thought would work for that purpose (Pattern.compile("\\s+\\w+\\." + "SITE_ID" + "\\s+")
and then, eventually, extract a capture) but it not returning the result I expected, I decided to simplify.
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 sql
variable, it still returns false, but sql.indexOf()
returns a value greater than -1, so sql
does contain the string:
boolean foundSiteId = Pattern.compile("SITE_ID").matcher(sql).matches(); // false
int siteIdPos = sql.indexOf("SITE_ID"); // 12
I find this surprising; it's not as though I'm trying to anchor "SITE_ID" to the front with ^
or the end with $
. Additionally I have gone out to https://www.freeformatter.com/java-regex-tester.html (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.
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.