用于将 mysql sql 语法转换为 sqlite 兼容语法的正则表达式(简单情况)
我想将以下 sql 查询: 转换
'UNIQUE KEY `UNIQUE_TAG` (`identifier`,`keyname`)'
为:
'CONSTRAINT `UNIQUE_TAG` UNIQUE (`identifier`,`keyname`)'
我的问题是,我必须将第一个关键字作为 var 获取,如下所示:
'<$1> KEY `UNIQUE_TAG` (`identifier`,`keyname`)'
在这里获取它
'CONSTRAINT `UNIQUE_TAG` <$1> (`identifier`,`keyname`)'
看看 http://www.sqlite.org/lang_createtable.html 查看 sqlite 兼容 SQL 的语法。
我想用 PHP 来做,但我不知道如何完成。
提前致谢。
i want to convert the following sql query:
'UNIQUE KEY `UNIQUE_TAG` (`identifier`,`keyname`)'
into this:
'CONSTRAINT `UNIQUE_TAG` UNIQUE (`identifier`,`keyname`)'
My problem is, i have to grab the first keyword as a var, like this:
'<$1> KEY `UNIQUE_TAG` (`identifier`,`keyname`)'
to get it here
'CONSTRAINT `UNIQUE_TAG` <$1> (`identifier`,`keyname`)'
Have a look at http://www.sqlite.org/lang_createtable.html to see the syntax of sqlite compatible SQL.
I want to do it in PHP, but i have no idea how to get this done.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的语法相差不远。
这个例子应该有帮助:
一些解释:
\b
匹配 单词边界(字符串或空格的开头)[^ ]
匹配非空格字符,[^ ]+
匹配非空格字符一个或更多时候(...)
捕获...
的内容。此捕获组可以在替换字符串中通过$1
引用(因为它是第一个捕获组)[^)]+
匹配非右括号字符的字符串,即至少一个字符长可读性较差但更通用的匹配:
进一步说明:
\w+
匹配一个或多个“单词”字符\h+
匹配一个或多个水平空白字符(制表符)和空格)\H
匹配一个或多个非“水平空白”字符(反引号不是单词字符)Your syntax wasn't far off.
This example should help:
Some explanation:
\b
matches a word boundary (beginning of the string or whitespace)[^ ]
matches non-space characters and[^ ]+
matches non-space characters one or more times(...)
captures whatever...
is. This captured group can be referenced in the substitution string by$1
(since it's the first capture group)[^)]+
matches a string of non-closing parenthesis characters that is at least one character longLess readable but more generalized matching:
Further explanation:
\w+
matches one or more "word" characters\h+
matches one or more horizontal whitespace characters (tabs and spaces)\H
matches one or more non-"horizontal whitespace" characters (backticks aren't word characters)试试这个正则表达式
(UNIQUE )?KEY (.+) (\(.+\))
并替换为CONSTRAINT $2 $1$3
http://www.myregextester.com/?r=83cd23ec
Try this regex
(UNIQUE )?KEY (.+) (\(.+\))
and replace withCONSTRAINT $2 $1$3
http://www.myregextester.com/?r=83cd23ec