I need to split a given multi-line string (sample here) using a regular expression with Swift. With PHP (the language I use for building web applications) I do this (after removing comments and substituting some tokens):
// split the file contents in fragments
$fragments = preg_split("#\n\s*\n#Uis", $contents);
And this is what I get if execute preg_split
over the code sample:
Array
(
...
[2] =>
CREATE TABLE IF NOT EXISTS c_search_history (
entry_id BIGINT(64) UNSIGNED NOT NULL AUTO_INCREMENT,
entry_date_added DATETIME NOT NULL,
entry_language CHAR(2) NOT NULL DEFAULT 'es',
entry_query TEXT NOT NULL,
PRIMARY KEY (entry_id),
INDEX (entry_date_added),
INDEX (entry_language)
) ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
[3] =>
CREATE TABLE IF NOT EXISTS c_search_weight_entries (
entry_id BIGINT(64) UNSIGNED NOT NULL AUTO_INCREMENT,
entry_date DATETIME NOT NULL,
entry_model VARCHAR(175) NOT NULL,
entry_model_id BIGINT(64) UNSIGNED NOT NULL,
entry_value DOUBLE NOT NULL,
PRIMARY KEY (entry_id),
INDEX (entry_date),
INDEX (entry_model),
INDEX (entry_model_id),
INDEX (entry_value)
) ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
...
)
The goal for this is to take an .sql file and flatten its contents so each function, table, procedure, trigger or view definition becomes a one-liner for later execution. The problem is I'm fairly new to Swift and I haven't been able to translate the #\n\s*\n#Uis
regular expression into something usable for NSRegularExpression.
I tried to use this solution but it outputs nothing for me (but I may be that I have used it in the wrong way or I don't understand how it really works).
Can you give me a hint? Any help would be greatly appreciated :)