Using curly brackets({}) for REGEX in drupal db_query - drupal

I have a where clause in my query like this "WHERE sth REGEXP '[0-9]{5,10}'"
when I run this query in phpmyadmin it returns all matched records but in drupal it has no result.I think it's because drupal assumes everything like "{sth}" as a table.
how can I solve this problem?
Thanks

Your theory is correct.
Curly brackets used as repetition quantifier in regexes are removed as any other curly bracket. Pass the regex as an argument to db_query() instead like this:
db_query('SELECT name from {users} WHERE std RLIKE "%s"', '[0-9]{5,10}');
(I've had to guess at the rest of your query.)

Related

teradata sql match regular expression

My code current strips: https://www.website.com/events
but I want to strip everything up to the /events, so it would be
https://www.website.com
I feel like I am close, but I am missing something ?
This is in Teradata SQL.
select 'https://www.website.com/events/143403?sid=1090794&mid=35' as string_to_search
,REGEXP_SUBSTR(string_to_search, '^.*(?=(/))',1,1,'i') as extract_domain
If you always got the scheme in your URLs you can use a simple
strtok(PAGREF_URL, '/',2)
Starting from the beginning of the string ^, take everything .* up to the 1st place ? where there is
No slash behind (?<!/)
A slash in front (?=/)
but not 2 slashes in front (?!//)
select 'https://www.website.com/events/143403?sid=1090794&mid=35' as string_to_search
,regexp_substr(string_to_search, '^.*?(?<!/)(?=/)(?!//)') as extract_domain
;
https://www.website.com

SQLite: trim usage

My goal is to extract the domain out of given URL.
For that end I use the following:
select distinct ltrim(rtrim('https://www.youtube.com/watch?v=...', '/'), 'https://')
The result I get is:
www.youtube.com/watch?v=...
While the following is expected:
www.youtube.com
How can the above be achieved?
Note:
I notices that the trim function works differently than I expected.
select distinct ltrim('https://www.youtube.com/watch?v...', 'youtu') returns the same string without any change.
Trying to trim only the slash by select ltrim('https://www.youtube.com/watch?v...', '/') returns the same string as well.
Any explainations are welcomed.
Trim only removes the given characters at the beginning and/or end of the string.
You'll need substr and instr. (https://www.sqlite.org/lang_corefunc.html)
But the best option is probably to fix this in your code before saving it to the database.
At the end I didn't use trim but substr as offered.
The following worked:
select replace(substr(substr(<url>, instr(<url>, '//')+2),0,instr(substr(<url>, instr(<url>, '//')+2),'/')),'.','')
select replace(substr(substr(<url>, instr(<url>, '//www.')+6),0,instr(substr(<url>, instr(<url>, '//www.')+6),'/')),'.','')

What's the difference between LIKE and GLOB in SQLite?

What the difference in the following to query ?
FROM COMPANY WHERE ADDRESS GLOB '*-*';
FROM COMPANY WHERE ADDRESS LIKE '%-%';
I know unlike LIKE operator, GLOB is case sensitive. Is it the only difference ?
The documentation says:
The GLOB operator is similar to LIKE but uses the Unix file globbing syntax for its wildcards. Also, GLOB is case sensitive, unlike LIKE.
And that's it.
Other difference that GLOB you can use it as regular expression
i.e. : to select fields which end with number use GLOB '*[0-9]'
to select fields which doesn't contain any number use GLOB '[^0-9]

swi-prolog odbc error while inserting into postgresql

I have the following problem and I'm begging for help: I'm using swi-prolog and odbc interface to connect to postgresql database. Problem occurs when I try to insert in database. SELECT works fine but INSERT doesn't work. Does anybody know what am I doing wrong.
Here's my simple test code:
:-use_module(library(odbc)).
connect(C):-
odbc_connect(baza, C, [user(Mat),
password(lozinka), alias(baza), open(once)]).
sel(R) :-
odbc_query(baza,
'SELECT * FROM pacijent',
R).
ins:-
odbc_query(baza, 'INSERT INTO pacijent (name, surname, passw, number) VALUES ("James", "Bond", 007, 007)').
This is the error when i try to insert:
?- ins.
ERROR: ODBC: State S1000: [unixODBC]ERROR: column "James" does not exist at character 30;
Error while executing the query
Also i tried to insert through psql console and everything works fine, but as said problem is when inserting from prolog.
Please help, im stuck here.
Thanks
A suggestion: proper escaping, don't know if it'll be enough though. I'd turn
"James", "Bond"
into
\'James\', \'Bond\'
The problem is you've passed the column data for columns name and surname in double quotes instead of single quotes. Most databases use " (double quotes) for identifiers like table and column names and ' (single quotes) for data. How you ensure that in prolog I don't know.
I suspect you'll want to quote the other 2 columns as well.
In ODBC you'd use SQLGetInfo and get SQL_IDENTIFIER_QUOTE_CHAR which will usually return double quotes meaning to quote identifiers use these quotes.

Oracle REGEXP_LIKE Strange Behavior

Have a simple query it's failing for one set of parameters but it works for other parameters it's not working.
This works:
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Encounters'
AND REGEXP_LIKE ('tplmcoce.41.20170822.txt', R.FILEMASK, 'i')
This is not working
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Facets'
AND REGEXP_LIKE ('SMS-0162628062', R.FILEMASK, 'i')
If I have a column called Filemask (REGEX Pattern) in database so how can I select matching pattern for given string (file name)?
When I try the second query I am getting the following exception:
ORA-12725: unmatched parentheses in regular expression 12725. 00000 - "unmatched
parentheses in regular expression"
*Cause: The regular expression did not have balanced parentheses.
*Action: Ensure the parentheses are correctly balanced.
SELECT R.*
FROM ROUTEUSER.AHC_B2B_ROUTE R
WHERE R.PRODUCER = 'Facets'
AND REGEXP_LIKE ('^SMS[-]0162628062$*', R.FILEMASK, 'i') ---- Try and let us know output......................
Here is a thought...
select *
from <your_table>
where regexp_count( FILEMASK, '\(' ) != regexp_count( FILEMASK, '\)' )
;
You should find at least one where PRODUCER = 'Facets' (and perhaps more?)
... although this may not be enough. Consider this regexp: '\(abc)' (where you meant '\(abc\)'). You want literal parentheses around the string abc. But you only escaped the opening parenthesis and forgot to escape the closing one. For a regular expression, that is an error: the escaped parenthesis is treated as any other character, it is not "special" in any way; this leaves the closing parenthesis as a special character, and it is not matched by an opening one.
On the other hand, my solution above is not able to distinguish between escaped and non-escaped parentheses - it treats them all the same, so '\(abc)' looks perfectly fine to it. If what I posted above is not enough, you/we will need more subtle ideas. Perhaps:
...
where regexp_count( FILEMASK, '([^\]|^)\(' ) != regexp_count( FILEMASK, '([^\]|^)\)'
This only looks for non-escaped parentheses. [^\] means "any character other than a backslash", and ([^\]|^) means either that or the beginning of the string.
This issue is resolved. This is because I had one bad regular expression (missed one parenthesis) which caused the failure. The REGEX_LIKE started comparing each mask in table column when it come to the bad regex it failed.
Please close this question.
Thanks all.
Escape the Chars in the [] with '\' . And you have to replace the last char '.' with * in the second pattern('SMS...')
^tplmcoce[\.](37|41|47|57)[\.].*[\.]txt$
and
^SMS[\-]0162628062$*
SELECT 1 Matched
FROM DUAL
WHERE REGEXP_LIKE ('tplmcoce.41.20170822.txt',
'^tplmcoce[\.](37|41|47|57)[\.].*[\.]txt$', 'i');
SELECT 1 Matched
FROM DUAL
WHERE REGEXP_LIKE ('SMS-0162628062', '^SMS[\-]0162628062$*', 'i')

Resources