How to properly escape colon in QueryExecute sql statement? - coldfusion

Update: Not satisfied with this answer but I found that not passing a param struct will cause CF2016 to ignore colons. Looks like CF2018 doesn't have the issue either way (though I can only test that with query of query at the moment).
We have generated sql queries that do not use query params going through QueryExecute(). I am sometimes seeing the error Space is not allowed after parameter prefix ':' and found it is caused by a string literal with a colon and space. For example:
select 'test this: error'
I was not able to find an official way to escape the colon and the common escapes didn't work, but figured out this workaround...
sqlstring = replace(sqlstring, ": ", ":'+' ", "all")
However that doesn't account for other situations that could potentially come up that wouldn't be a string literal such as a column name with colon and space and likely many more I can't think of at the moment.
Is there an official way to escape a colon passed to QueryExecute not part of a queryparam?

I suppose you could separate the string out and have it be passed in
result = QueryExecute("
SELECT :mystring AS ...
",
{ mystring : "test this: error"}
);
Seems like a lot of work though.

Related

Regex to match forward slash surrounded by double quotes

I have a serialised string that comes from Spring hosted end-points. On the frontend which is javascript based, I wanted to prettify the serialised string that comes from API to a string that is parsable through JSON.parse();
Please let me know the regex to match and replace the required fields as below.
sample string: \"address\":\"<VALUE>"\"}, I want to replace all the instances of "\" which comes at the end of VALUE with \"
Tried doing this: str.replaceAll('\"/\\\"', '/\\\"') but no luck.
Here is the code, we have to escape characters to put the wanted values into the variable:
testString='\\\"address\\\":\\\"<VALUE>"\\\"},';
alert(testString);
alert(testString.replace(/\"\\\"/,'\\\"'));
The first alert gives us the originale testString:
\"address\":\"<VALUE>"\"},
and the second the modified testString
\"address\":\"<VALUE>\"},
Tested with https://www.webtoolkitonline.com/javascript-tester.html

Python regex to ignore what is between quotes

I have a string, part of which is surrounded within quotes. Like the one at the third line of the code snippet below. I want the string to be formatted into a dict literal. Meaning wherever the quotes are missing, they should be added. But the part which is within the quotes has to be ignored. I came up with the code below to handle this:
from ast import literal_eval
from re import sub
str = "key1:[val1,val2,val3],key2:'val4A,val4B'"
str = sub(r"([\w\-\.]+|[\"'].*[\"'])", r"'\1'", f"{{{str}}}")
str = sub(r"[\"']{2,}(.*)[\"']{2,}", r"'\1'", str)
fin = literal_eval(str)
print(fin)
This code does the work, but I want to know if there is a way to achieve this with one time usage of sub. Before you mark this as a duplicate, I tried a large number of the solutions provided on the web including positive and negative look ahead and look behind, exclusion, and simple negative match. Couldn't find any which would work. If there is a solution I have missed or anyone has a solutions, I would highly appreciate knowing about it.
Try this ([\w\-\.]+(?=(?:[^']*'[^']*')*[^']*$)) :
Live Demo

SQLite - display table names finishing by "_1"

I am looking for a way to display table names I have in a database for which the name is ending by "_1".
I tried to use the command:
.tables '%_1';
Unfortunately the underscore symbol is used in the expression matching, so it returned me tables such as:
"125_1","125_11","125_21".
Only the first one is interesting in this example, I will not display the full result because there are hundreds of tables. So I tried something like this:
.tables '%_1' ESCAPE '_';
And it gave me the exact same result.
If you have a solution to overcome the problem of the underscore symbol, please post it.
remember that I have hundreds of tables with names following this pattern in regex: "^\d+_\d+$"
This is not how the ESCAPE clause works. To search for an underscore, you must escape the underscore with the escape character:
LIKE '%#_1' ESCAPE '#'
Anyway, .tables is not an SQL command and ignores the ESCAPE clause. To do your own search, you have to run your own query:
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name LIKE '%#_1' ESCAPE '#';

regex to match everything except character

I have a payload that contains the following:
����\�p�a�t�r�i�c�k�-�t�e�s�t�-�f�i�l�e�.�t�x�t������x�SMB2
I'm looking to extract the file name of patrick-test-file.txt
I can get close by using this, but it continues to include everything (including ascii characters)
[\\\\](.*?)x�SMB2
With a result of this: �p�a�t�r�i�c�k�-�t�e�s�t�-�f�i�l�e�.�t�x�t������ for the capture group.
How would I just match the characters of the file name, which could be anything of variable length, and could contain alphanumeric characters? Is this possible with pure regex?
Any help is much appreciated.
Sometimes you just can't do a single language-agnostic Regular Expression to accomplish something. And sometimes (usually) it is more performant to do a series of string functions.
I wouldn't personally accept any solution which has hard-coded values, such as x�SMB2.
If you want to use Regular Expressions only, you can first select the File-Name portion like so: (([-\w\d.\\]+)[^-\w\d.\\]?)+, then go ahead and replace [^-\w\d.\\] with nothing "".
Honestly, given the limited detail, the best function is like so:
var fileName = "\patrick-test-file.txt";
But half-joking aside, and with that limited detail, your best bet is to do a couple string functions:
var yuckyString = #"����\�p�a�t�r�i�c�k�-�t�e�s�t�-�f�i�l�e�.�t�x�t������x�SMB2";
var fileNameArea = yuckyString.Split(new[] { "��" }, StringSplitOptions.RemoveEmptyEntries)[0];
var fileName = fileNameArea.Replace("�", "");
Granted, there was no language listed, so I'm using C#. Also, the answer would change if there were irregularities with those special characters. With the limited info, the pattern seems clear.

Regex URI portion: Remove hyphens

I have to split URIs on the second portion:
/directory/this-part/blah
The issue I'm facing is that I have 2 URIs which logically need to be one
/directory/house-&-home/blah
/directory/house-%26-home/blah
This comes back as:
house-&-home and house-%26-home
So logically I need a regex to retrieve the second portion but also remove everything between the hyphens.
I have this, so far:
/[^(/;\?)]*/([^(/;\?)]*).*
(?<=directory\/)(.+?)(?=\/)
Does this solve your issue? This returns:
house-&-home and house-%26-home
Here is a demo
If you want to get the result:
house--home
then you should use a replace method. Because I am not sure what language you are using, I will give my example in java:
String regex = (?<=directory\/)(.+?)(?=\/);
String str = "/directory/house-&-home/blah"
Pattern.compile(regex).matcher(str).replaceAll("\&", "");
This replace method allows you to replace a certain pattern ( The & symbol ) with nothing ""