Hi i have the following group:
`group_id` int(10) unsigned NOT NULL,
`right_id` int(10) unsigned NOT NULL,
`group__right_value` enum('allow','deny') NOT NULL DEFAULT 'deny',
KEY `group_id` (`group_id`),
KEY `right_id` (`right_id`)
And i expected the strings to always end with a comma so a did a split based on that. Now i've noticed that in some cases like above the commas should be ignored and the values left as they are:
enum('allow','deny')
How should i exclude the commas between brackets when doing the split?
EDIT:
The expected result should be:
'group_id' int(10) unsigned NOT NULL,
'right_id' int(10) unsigned NOT NULL,
'group__right_value' enum('allow','deny') NOT NULL DEFAULT 'deny',
KEY 'group_id' ('group_id'),
KEY 'right_id' ('right_id')
Matching a comma at the end of the string is done with an anchor:
/,$/m
The /m modifier is used to enable multiline mode, in which $ will match the end of the line instead of the standard end-of-subject.
http://regexr.com?3579s
Hope this help [^\,]+(\,\')?[^\,]+
Related
Can we use Regex i.e, Regular Expression in SQL Server? I'm using SQL-2012 and 2014 and there is an requirement to match and return input from my stored procedure.
I can't use LIKE in this situation since like only returns matching words, Using Regex I can match whole bunch of characters like Space, Hyphen, Numbers.
Here is my SP
--Suppose XYZ P is my Search Condition
Declare #Condition varchar(50) = 'XYZ P'
CREATE PROCEDURE [dbo].[usp_MATCHNAME]
#Condition varchar(25)
as
Begin
select * from tblPerson
where UPPER(Name) like UPPER(#Condition) + '%'
-- It should return both XYZ P and xyzp
End
Here my SP is going to return all matching condition where Name=XYZ P, but how to retrieve other Column having Name as [XYZP, XYZ-P]
and if search condition have any Alphanumeric value like
--Suppose XYZ 1 is my Search Condition
Declare #Condition varchar(50) = 'XYZ 1'
Then my search result should also return nonspace value like [XYZ1, xyz1, Xyz -1].
I don't want to use Substring by finding space and splitting them based on space and then matching.
Note: My input condition i.e., #Condition can have both Space or Space less, Hyphen(-) value when executing Stored Procedure.
Use REPLACE command.
It will replace the single space into %, so it will return your expected results:
SELECT *
FROM tblPerson
WHERE UPPER(Name) LIKE REPLACE(UPPER(#Condition), ' ', '%') + '%'
I've been trying the following but it does not return any (null) values. How can I include null values in using a regular expression with oracle an in fact return everything including null. (It is necessary to use a regex as the value is replaced with a variable in python and so the value will not always be null. Otherwise i could just leave out the expression.)
SELECT * from table WHERE
REGEXP_LIKE (column1,'.*|NULL$')
Two methods.
Explicit comparison:
WHERE (REGEXP_LIKE(column1, '.*') OR column1 IS NULL)
Or replacement:
WHERE REGEXP_LIKE(COALESCE(column1, '<NULL>'), '.*|<NULL>')
The second method is more dangerous, because you could already have the replacement string in the column.
I am writing a script to extract and convert SQL statements from a file. I need to convert the sql unloaded froma gupta sqlbase database into sql that SQLServer can understand.
One task is to replace keywords that are not allowed as column names with a compatible name.
In the following code $commands is an array ref that contains sql statements. (There is actually more code here, but I extracted it because it shouldn't be relevant here)
my #KeyWords = ("LEFT", "RIGHT", "PERCENT", "FILE", "PRINT", "CROSS", "PLAN", "TOP", "END", "FILE", "Default", "CHECK", "TEXT");
foreach $cmd (#$commands) {
foreach my $kw (#KeyWords) {
$cmd =~ s/\b$kw\b[^(]/_$kw/gi;
}
push #$converted, $cmd;
}
This works fine for most statements but in the following command "DEFAULT" gets replaced with "_DEFAULT instead of "_DEFAULT". So the second quotation mark is lost.
CREATE TABLE SYSADM.SUBTYPE ( ID_SUBTYPE INTEGER NOT NULL,
ID_TYPE INTEGER NOT NULL,
TYPE VARCHAR(1),
BEZEICH VARCHAR(60),
NUM_COLOR INTEGER,
NUM_TXTCOLOR INTEGER,
"DEFAULT" SMALLINT,
GENER_ARBA SMALLINT,
PROJEKTPLANUNG SMALLINT)
Is there a way to modify the regular expression/substition so this will not remove the second quotation mark? Or an other way?
[^(] matches any single character that is not a left opening paranthesis.
You want to use a negative zero-width lookahead assertion instead:
s/\b$kw\b(?!\()/_$kw/gi;
(Alternatively: (?![(]))
You can also add the replaced character back to the string:
s/\b$kw\b([^(])/_$kw$1/gi;
But note that this will not work in all cases. Especially if there is nothing after the keyword, this pattern will not match whereas zero-width assertion will.
I have been working on some legacy code in our application that detects certain keywords in the text of an SQL stored procedure by using Regex and I have found a bug that I can't quite correct due to my limited knowledge of Regex.
Basically the regex that I currently have works in all but one case:
(?<=\n\s*)(?<!with.*[\s\S]*)as
It should return a match on this version of a stored procedure:
ALTER PROCEDURE [dbo].[p_obj_name_with_something]
#username [nvarchar](100) = null,
#id [int] = null,
#mode [int] = 0
AS
/*-------------------------------------------------------------------------
However it shouldn't for this version, but it currently does return a match:
ALTER PROCEDURE [dbo].[p_obj_name_with_something]
#username [nvarchar](100) = null,
#id [int] = null,
#mode [int] = 0
WITH EXECUTE AS CALLER
AS
/*-------------------------------------------------------------------------
I want a match when the keyword WITH isn't found before the AS keyword, but it will allow the word within the name or parameters of the stored procedure.
The way I think the detection would work is if the keyword WITH has whitespace (or a newline) either side of it, but I can't quite figure out the regex syntax.
Any suggestions?
Check out this site:
http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet
notice the lines:
\s Matches any white space including space, tab, form-feed, etc. Equivalent to "[ \f\n\r\t\v]".
[^xyz] A negative character set. Matches any character not enclosed. For example, "[^abc]" matches the "p" in "plain".
so.....
[\s]+[[WITH]|[with][\s]+[[^AS]|[^as]]*
breakdown of it:
[\s]+ is the whitespace on either side
[[with]|[WITH][ case insensitive check for with
[[^AS]|[^as]]* case insensitive check that will match as long as AS or as isnt there
hope this helps
Even though it isn't a real answer, I found the easier way to do this was to enforce some naming convention rules in our stored procedures and modify the 20 or so that violated the rules.
Indeed, this is a case when Regex is not the solution!
i have string that contains apostrophe and comma's and when i execute insert into SQLite
it gives me error for example with string like this :
...., 'The Smiths - I Know It's Over', .....
"Over": syntax error Unable to execute statement
how can i or what can i do to keep the apostrophe's in the string but preform valid insert?
im using Qt c++ .
You shouldn't be putting arbitrary strings directly into SQL - that's asking for an injection attack. Instead, use bound parameters; something like:
sqlite3_stmt * statement;
sqlite3_prepare(db, "select * from students where name=?", -1, &statement, NULL);
sqlite3_bind_text(statement, 1, "'; drop table students --", -1, SQLITE_STATIC);
sqlite3_step(statement);
sqlite3_finalize(statement);
This will replace the first parameter (?) in the query with the given string, with no danger of any ' character being interpreted as the end of the string, or of any part of the string being executed as SQL.
From the FAQ:
(14) How do I use a string literal that contains an embedded single-quote (') character?
The SQL standard specifies that single-quotes in strings are escaped by putting two single quotes in a row. SQL works like the Pascal programming language in the regard. SQLite follows this standard. Example:
INSERT INTO xyz VALUES('5 O''clock');
I believe the mysql library has a function named mysql_real_escape_string to escape the string. Also, you may use double quotes to surround the string or escape the apostrophe of your input string like this 'The Smiths - I Know It\'s Over',