Jump to content

StringRegExpReplace - What am I missing? [Solved]


icu
 Share

Recommended Posts

Dear AutoIt Community,

I'm getting SQLite errors because of the single quote character in a string (').

I figured I could escape it out using a Func based on StringRegExpReplace however it doesn't work and I just don't see why. Here is my code:

Func SQLite_Regex (ByRef $string)
$value = StringRegExpReplace ($string, ".('+).", "''")
Return $value
EndFunc
 
$s_Test = "De L'isard's"
ConsoleWrite ("Before SQLite_Regex: " & $s_Test & @CRLF)
$s_Result = SQLite_Regex ($s_Test)
ConsoleWrite ("After SQLite_Regex: " & $s_Result & @CRLF)

I was pretty sure I escaped the single quotes with the double quotes enclosure.

Any and all help is greatly appreciated.

Kind regards,

icu

Edited by icu
Link to comment
Share on other sites

  • Moderators

StringReplace would be faster:

StringReplace($string, "'", "''", 0, 1)

But... Your question:

StringRegExpReplace($string, "'", "''")

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 1 month later...

Single quote character (') for Europeans (and not english speaking countries in general ) is a problem.

A lot of systems have words with accent (mark) , and with sqlite I have to cope with this situations.

For a long time I used this function :

Func apostrofo($letto)

$letto = StringRegExpReplace($letto, "(w)(')(w)", "1223") ; tra caratteri ;perla ; pearl
$letto = StringRegExpReplace($letto, "(w)(')(s)", "1223") ; carattere prima e dopo spazio carattere
$letto = StringRegExpReplace($letto, "(s)(')(w)", "1223") ; spazio e poi carattere
$letto = StringRegExpReplace($letto, "(s)(')(s)", "1223") ; spazio e poi spazio
$letto = StringRegExpReplace($letto, "(w)(')(Z)", "122") ; ultimo carattere è apostrofo
$letto = StringRegExpReplace($letto, "(A)(')(w)", "223") ; primo carattere è apostrofo
$letto = StringRegExpReplace($letto, "(s)(')(Z)", "122") ; ultimo carattere è apostrofo prima di spazio
$letto = StringRegExpReplace($letto, "(A)(')(s)", "223") ; primo carattere è apostrofo e dopo uno spazio
$letto = StringRegExpReplace($letto, "(w)(')(,)", "1223") ; carattere e poi virgola ;perla ; pearl , scoperto in tortora.au3
$letto = StringRegExpReplace($letto, "(,)(')(w)", "1223") ; virgola e poi carattere ;perla ; pearl , scoperto in tortora.au3
$letto = StringRegExpReplace($letto, "(w)(')(-)", "1223") ; carattere e poi - ;perla ; pearl , scoperto in tortora.au3
$letto = StringRegExpReplace($letto, "(w)(')(()", "1223") ; carattere e poi ( ;perla ; pearl , scoperto in tortora.au3

Return $letto
EndFunc   ;==>apostrofo

Until now, when I have problems with the last line (w)(')(()", "1223") where the (() don't work at all ... ?

But I found this thread and I think I was blind for not seeing a very simple and fast solution like that.

thankS !

So I don't use anymore my old function, but for the ()) in regular expression anyone has a solution ?

(sorry for the english...)

Link to comment
Share on other sites

You're making your life more difficult that it should be!

Your (() should be (() : remember that ( is an active token in regexps.

Now, you should always escape each literal string in SQL statements using StringReplace($s, "'", "''"). The SQLite UDF doesn't offer data binding since it reveals much slower than SQLite_Exec in almost all practical uses.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...