I can use regexp to list hits from a Sqlite3 db, but what is the syntax for a "search and replace" using regexp.
+1
A:
If you are thinking of using backreferences in the replacement string, that's not possible, AFAIK. You do an UPDATE, as follows:
UPDATE foo
SET bar = <some expr including baz>
WHERE baz REGEXP <regex>
But the assigned expression will have to rely conventional string functions like replace(...)
and substr(...)
(or your own extension functions). There is no way to invoke groups found by the REGEXP
operator.
EDIT: Here's a concrete example that interprets numeric stock IDs following the prefix 'STOCK ID: '
in the item_key
column as stock numbers:
UPDATE staff
SET stock_number = CAST(substr(item_key, 11) AS INTEGER)
WHERE item_key REGEXP '^STOCK ID: \d+'
Marcelo Cantos
2010-10-16 10:58:44
Thanks but I don't fully understand the formatting and how it all applies where you have <some expr including baz> and <regex>. Can you annotate the example fully. Thanks
JPG
2010-10-16 12:19:34
@JPG: I've added an example to my answer.
Marcelo Cantos
2010-10-17 06:22:49
+1 for link to extension functions. This is the way to do what the OP wants.
Mark Byers
2010-10-17 07:29:58
Thank you for the working example.
JPG
2010-10-17 12:56:07