Regex: match SQL PRINT blocks with quoted text in it - sql

I have the following text I am trying match using regular expressions:
PRINT CONVERT(NVARCHAR,
CURRENT_TIMESTAMP, 111) + ' ' +
CONVERT(NVARCHAR, CURRENT_TIMESTAMP,
108)
+ ' -Test Mode : ' + (CASE WHEN #turbo_mode_ind = 1 THEN
'some text ''test'' some more text.'
ELSE 'and even more text ''temp'' when
will it stop ?' END)
PRINT 'text don''t text'
PRINT 'text ''test2'' text'
What I want to match is:
PRINT CONVERT(NVARCHAR,
CURRENT_TIMESTAMP, 111) + ' ' +
CONVERT(NVARCHAR, CURRENT_TIMESTAMP,
108)
+ ' -Test Mode : ' + (CASE WHEN #turbo_mode_ind = 1 THEN
'some text ''test''
PRINT 'text ''test2''
So basically I want to match:
starting at PRINT
each char that comes after PRINT (.*)
inclusive line-breaks (don't stop at
line-breaks)
with \'{2}\w+\'{2} at the end of the
match
non-greedy (.*?)
AND no empty line(s) between PRINT
and \'{2}\w+\'{2}
I have already compsed this, but it still matches empty line(s):
PRINT.*?\'{2}\w+\'{2}(?!\n\s*\n)

Edit after comment:
Looking at the requirements again I could not come up with a single regex solution quickly. In your comments you mention that you are using C#.
A possible solution would therfore be to first split the string at blank lines and then extracting the text.
Something like this:
string pattern = #"^$";
foreach (string result in Regex.Split(input, pattern, RegexOptions.Multiline)
{
Regex rxFindSql = Regex(#"PRINT.*?\'{2}\w+?\'{2}", RegexOptions.SingleLine)
MatchCollection matches = rxFindSql.Matches(result);
}
This should do the trick but I did not test the code.
I hope this helps.

Related

GROUP_CONCAT() error in h2 [42001-214] , [42122-214]

i keep getting this error GROUP_CONCAT(""""[*],a.montant, a.type_avance,a.date_avance,
a.remark SEPARATOR '') as Avance [42001-214] ,and after
i removed GROUP_CONCAT to test the code i got the error:
Column "" not found; SQL statement [42122-214]
here is my code:
SELECT i.n_dossier , concat(\"<html>\",i.nom_prenom,\"<br></html>\")
,concat(\"<html>\",i.vehicule,\"<br></html>\"),i.prime_totale ,"
+ " i.date_effet , i.date_echean ,GROUP_CONCAT(\"<html>\",a.montant,"
+ " a.type_avance,a.date_avance,a.remark SEPARATOR \"<br>\") as Avance ,i.reste ,i.GSM,"
+ "i.observation ,\"</html>\" FROM info_impayee i LEFT JOIN avance a ON i.n_dossier = "
+ " a.n_dossier GROUP by i.n_dossier,i.date_dossier,i.anuller having i.anuller = ' active '
You must repalce
GROUP_CONCAT("<html>",a.montant, a.type_avance,a.date_avance,a.remark SEPARATOR "<br>")
with
GROUP_CONCAT(CONCAT("<html>",a.montant, a.type_avance,a.date_avance,a.remark) SEPARATOR "<br>")
GROUP_CONCAT needs to have 1 column, as you see in the samle CONCAT will do that.
I don't why you use single quorted ' active 'and doou

numpy/pandas - why the selected the element from list are the same by random.choice

there is a list which contains integer values.
list=[1,2,3,.....]
then I use np.random.choice function to select a random element and add it to the a existing dataframe column, please refer to below code
df.message = df.message.astype(str) + "rowNumber=" + '"' + str(np.random.choice(list)) + '"'
But the element selected by np.random.choice and appended to the message column are always the same for all message row.
What is issue here?
Expected result is that the selected element from the list is not the same.
Pass to np.random.choice with parameter size and convert values to strings:
df = pd.DataFrame(
{'message' : ['aa','bb','cc']})
L = [1,2,3,4,5]
df.message = (df.message.astype(str) + "rowNumber=" + '"' +
np.random.choice(L, size=len(df)).astype(str) + '"')
print (df)
message
0 aarowNumber="4"
1 bbrowNumber="2"
2 ccrowNumber="5"

Is there any LTRIM function for MS ACCESS 2007?

i am trying to ltrim 3 or 4 zeros from a column in Access 2007 but not getting any result where the data-type is text(from a csv data).
From what I read, MS Access in fact should support LTRIM. Here is a workaround in case you need it:
SELECT
IIF(string LIKE "0000*",
MID(string, 5),
IIF(string LIKE "000*", MID(string, 4), string)) output
FROM yourTable;
Here are a couple of quickly written LTrim & RTrim functions to operate with characters other than spaces:
Function LTrimChr(strStr As String, strChr As String) As String
If strStr Like strChr & "*" Then
LTrimChr = LTrimChr(Mid(strStr, 2), strChr)
Else
LTrimChr = strStr
End If
End Function
Function RTrimChr(strStr As String, strChr As String) As String
If strStr Like "*" & strChr Then
RTrimChr = RTrimChr(Left(strStr, Len(strStr) - 1), strChr)
Else
RTrimChr = strStr
End If
End Function
Expects the strChr to be a single character, e.g.:
?LTrimChr("000123456789000","0")
123456789000
?RTrimChr("000123456789000","0")
000123456789
In case of digits only, you can use Val to strip leading zeroes:
LTrimmedValue = CStr(Val(Value))
' "000123456789000" -> "123456789000"
MS Access supports ltrim(), but only for spaces. So, assuming that your string has no spaces, you can use replace() and ltrim():
select replace(ltrim(replace(col, '0', ' ')), ' ', '0')
If you do have spaces, there is often a character you don't have, so you can replace the spaces first:
select replace(replace(ltrim(replace(replace(col, ' ', '~'
), '0', ' '
)
), ' ', '0'
), '~', ' '
)

How to replace {“bcz”,u,thr} with {“because”,you,there} in whole text? (Text-ming)

Can someone please explain with example R/Python code to replace {“bcz”,u,thr} with {“because”,you,there} in whole text? (Text-ming)
The remove_words function gets a string and returns another string with making your required changes. Make sure that regex package is installed properly to be able to run this code and get the desired output. Through regex you can define the compilers for extracting each pattern of strings you want to change:
import regex as re
def remove_words(my_line):
new_line =''
compiler_thr = re.compile(r"thr")
compiler_u = re.compile(r"u")
compiler_bcz = re.compile(r"bcz")
for i in my_line.split():
if i in compiler_thr.findall(my_line):
new_line = new_line + ' ' + 'there'
elif i in compiler_u.findall(my_line):
new_line = new_line + ' ' + 'you'
elif i in compiler_bcz.findall(my_line):
new_line = new_line + ' ' + 'because'
else:
new_line = new_line + ' ' + i
return new_line

Iterate through each string and its every character and make string using SQL?

I am making pattern for my strings in database here is my needs:
I have to iterate through each record and then I have to check its every character of string using SQL
After that I have to concatenate the string and show like that:
//DECLARING STRINGS IN VARIABLES
SET #str = '4444--6699p'
SET #CHS = '%[a-z]'
SET #CHE = '[a-z]'
SET #CHE2 = '[a-z]%'
SET #NUMS = '%[0-9]'
SET #NUME = '[0-9]'
SET #NUME2 = '[0-9]%'
SET #CHR = '-'
//GET THE IDENT OF SLASH
SET #INDENT = (SELECT PATINDEX('%-%', #str));
// CHECH CONTAIN CHARECTER OR NOT
IF PATINDEX('%[a-z]%' , #str) > 0
//BUILT STRING TILL INDENT
SET #CHA = #CHS
while #id <= #INDENT
begin
set #id = #id + 1
SET #CHA = #CHA + #CHE
end
SET #CHA = #CHA + #CHE2
print #CHA
end
//IF NO CHARECTER BUILT HERE
ELSE
print #NUMS + #NUME + #NUME2
but no want to concate string in that this is a pattern but here is explaination
have a look at database strings now
// DATABASE EXAMPLES OF STRING
(512) 482-2392
(518) 457-5181
http://www.menupages.com/restaurants/cafe-shane/
https://www.google.com/#q=auto
025-121-3453
429–432–2145
there expression returned as for 1 if record exist 3 times or more other wise null
(%[0-9][0-9][0-9]%) [0-9][0-9][0-9] - [0-9][0-9][0-9][0-9]
expressions can be like that abcz-aaassss-ccv so [a-z]{0,3}-[a-z]{0,10}-[a-z]{0,3}
HINT:
As in case of c sharp we do
string builtme;
builtme = builtme + builtme;
ACHEIVEMENT
HERE IS OUT PUT EXAMPLE
%[a-z][a-z][a-z]% - %[a-z][a-z][a-z][a-z]%-%[a-z][a-z][a-z]%