SQLite - How to use regex with if-condition? - regex

How can I use regex with if-condition in sqlite? My query below,
....
WHERE DATE(localdate) BETWEEN '2014-10-09' AND '2015-05-12'
AND n.nid = '9'
AND CASE WHEN '9' REGEXP '^[0-9]+$' THEN w.wspdi != '-9999' ELSE w.wspdi != NULL END
error,
no such function: REGEXP
Any ideas?

Related

Regular expression for duplicating conditions in every if statement

I'm writing a Regular Expression in Notepad++ to duplicate and modify certain if conditions.
For instance:
if (variable1 == "") should become
if (variable1 == "" or len(variable1) == 0)
The key things I need to match are the variable names until the =="" so I can duplicate them for the or condition
I had the following expression:
[A-Za-z0-9()]*?\s*==\s*""
But it fails when there is no white space between the If and the parentheses
https://regex101.com/r/Beyumi/3
I believe the following lines should cover most cases:
If (Trim(var1) == "" And var2 == "")
/*Then do something*
ElseIf(var3 == "" And var4 == "" And Trim(var5)=="") Then
/*block of code*/
ElseIf var6 ==""
The expression should be able to match:
Trim(var1) == ""
var2 == ""
var3 == ""
var4 == ""
Trim(var5)==""
var6 ==""
Note: In the event of And statements, I can manually add the necessary parentheses when adding the 'or' condition
You can use this regex,
((\w+|trim\(\w+\))\s*==\s*"")
and replace it with this,
(\1 or len(\2) == 0)
Notice, I've enabled case insensitive matching and (\w+|trim\(\w+\)) part matches either plain variable using \w+ or matches trim(variablename) using trim\(\w+\) and captures it in group2, where \s* around == takes care of optional whitespaces and whole group is captured in parenthesis so it can be replaced with current expression and additionally with or len(\2) == 0 and whole of it surrounded by parenthesis for appropriate grouping due to and condition.
Regex Demo

c# How to Split CSV string which have string with commas [duplicate]

This question already has answers here:
Reading CSV files using C#
(12 answers)
Closed 7 years ago.
I have below mentioned CSV string which I need to split using commas .
Input:
A,"Rakesh,Gaur",B,"A,B",Z
OutPut:
A
Rakesh,Gaur
B
A,B
Z
You can't use string split or regular expressions. If you are not going to use a library that is already built, you have to keep track of whether or not you are in_quotes. but as you will find out after you start this: csv parsing is complex. You should use something that is already pre-built. As I recall from my days writing an app that heavily relied on csv, there are escape characters and such, that you will need to account for.
Either way the psuedo code is as follows:
Stack cells = m
in_quotes = false
foreach character in string:
if character != ',' && character != '"':
cells.Top = cells.Top + character
else if character == ',' && in_quotes:
cells.Top = cells.Top + character
else if character == ',':
cells.push("")
else if character == '"' && in_quotes:
in_quotes = false
else if character == '"':
in_quotes = true
I think you can do this using following steps:
string[] words = yourStringInput.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}

how to have regular expression for a textfield which accepts all characters except a comma (,) and do not accept a white space at both ends

How to write a regular expression for a text field which accepts all characters except a comma (,) and do not accept a white space at both the ends? I have tried
[^,][\B ]
but no use
like 'product generic no' instead of 'product,generic,no' or ' product generic no '
I suggest a solution without regular expression. As you said you're using JS so the function is in JavaScript:
function isItInvalid(str) {
var last = str.length - 1;
return (last < 2 ||
str[0] == ' ' ||
str[last] == ' ' ||
str.indexOf(',') != -1);
}
EDIT: Just made it a bit more readable. It also checks if the string is at least 3 chars.
Something like below:
/^\S[^,]*\S$/
Using a Perl regular expression
/^\S[^,]*\S$/
This should work from 2 characters up, but fails in the edge case where the string has only one non-comma character. To cover that too:
/^((\S[^,]*\S)|([^\s,]))$/

Remove / character from my DDL query

I am using below query to get DDL of my function without getting the user name. In my query I am getting / slash also and I don't want to see the / too. How can I get rid off this / ?
FUNCTION_NAME= "EMP"
USER_NAME="USER1"
SELECT REGEXP_REPLACE (
REPLACE (
dbms_metadata.get_ddl ('FUNCTION', '" + function_name+ #"')
, '""" + User_name + #""".')
,'^\s+', NULL, 1, 0, 'm')
FROM dual
result:
CREATE OR REPLACE FUNCTION "EMP"
(str_in IN VARCHAR2) RETURN BOOLEAN AS
-- validating ###-##-#### format
BEGIN
IF TRANSLATE(str_in, '0123456789A','AAAAAAAAAAB') = 'AAA-AA-AAAA' THEN
RETURN TRUE;
END IF;
RETURN FALSE;
END ssn_candy;
/
Expected result
CREATE OR REPLACE FUNCTION "EMP"
(str_in IN VARCHAR2) RETURN BOOLEAN AS
-- validating ###-##-#### format
BEGIN
IF TRANSLATE(str_in, '0123456789A','AAAAAAAAAAB') = 'AAA-AA-AAAA' THEN
RETURN TRUE;
END IF;
RETURN FALSE;
END ssn_candy;
Your regular expression removes the empty spaces at the beginning of each line. You can modify it to '^(\s+|/)' to remove the slash at the beginning of the line.
You can also try the TRIM function instead of Regular Expressions, if your intention is to solely remove the slash.
trim (trailing '/' from 'source string')

Scala - how to filter list with two chars

I have a char List is Scala where I want to remove all chars that are not parentheses. The problem is I only seem to be able to do this for one character, eg:
var parens = chars.filter(_ == '(')
If I try this:
var parens = chars.filter(_ == '(').filter(_ == ')')
..I get nothing since I am filtering it once, then a second time which removes everything. How can I filter a character List (not a string list) for multiple chars?
If you need/want a functional solution then try this:
val givenList = List('(', '[', ']', '}', ')')
val acceptedChars = List('(', ')')
givenList filter acceptedChars.contains // or givenList.filter(acceptedChars.contains)
Now you can add whatever chars you like to the seconds list on which you wanna filter the given list without changing filter call. If you want to leave chars that are not in the acceptedList just change to filterNot. Another advantage of this aproach, is that you do not need to write big lambda functions combining all the chars on which you wanna filter like: x => x == '(' || x == ')' || etc.
Update
Like senia proposed in the comment you can also use shorter version with Set just change function acceptedChars.contains with a Set of given chars:
givenList.filter(Set('(', ')'))
This will remove all characters that are not parentheses:
val parens = chars.filter(c=>c=='('||c==')')
The following is what I tested in scala console:
scala> val chars = List('a', 'b', '(', 'c', ')', ')')
chars: List[Char] = List(a, b, (, c, ), ))
scala> val parens = chars.filter(c=>c=='('||c==')')
parens: List[Char] = List((, ), ))
The reason that your code removes everything is that... the first filter (chars.filter(_ == '(')) removes all the characters that are not (, which means only ( remains. Applying filter(_ == ')') to this result returns empty list.