简体   繁体   English

使用 PHP 等效的正则表达式 (#\n\s*\n#Uis) 使用 Swift 拆分字符串

[英]Splitting a string with Swift using a PHP-equivalent regular expression (#\n\s*\n#Uis)

I need to split a given multi-line string (sample here ) using a regular expression with Swift.我需要使用带有 Swift 的正则表达式拆分给定的多行字符串(此处为示例)。 With PHP (the language I use for building web applications) I do this (after removing comments and substituting some tokens):使用 PHP (我用于构建 web 应用程序的语言)我这样做(在删除注释并替换一些标记之后):

// 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:如果在代码示例上执行preg_split ,这就是我得到的:

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.这样做的目标是获取一个 .sql 文件并展平其内容,以便每个 function、表、过程、触发器或视图定义成为单行以供以后执行。 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.问题是我对 Swift 还很陌生,我无法将#\n\s*\n#Uis正则表达式转换为可用于 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:)任何帮助将不胜感激:)

After doing, undoing and trying different approaches, seems the only thing I needed to do was to substitute #\n\s*\n#Uis with \\n\\s*\\n and it worked.在完成、撤消和尝试不同的方法之后,似乎我唯一需要做的就是用\\n\\s*\\n替换#\n\s*\n#Uis并且它起作用了。 I still need to do some serious research on NSRegularExpression and a lot of other Swift concepts but, for now, that did the trick:)我仍然需要对 NSRegularExpression 和许多其他 Swift 概念做一些认真的研究,但是,就目前而言,这成功了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM