Home Backend Development PHP Tutorial 来问一个有关问题,正则匹配创建数据库SQL的数据库名称

来问一个有关问题,正则匹配创建数据库SQL的数据库名称

Jun 13, 2016 pm 01:34 PM
create database sql

来问一个问题,正则匹配创建数据库SQL的数据库名称?
要考虑所有情况,只要能创建数据库的SQL的名称都要匹配出来哦。
名称有可能没有``,有可能没有空格,有可能没有修饰关键字等...



------解决方案--------------------
/create\s+database\s+(\w+)/is

名称有可能没有``,有可能没有空格
无可能
------解决方案--------------------

PHP code
<?php $sql=<<<SQL
CREATE DATABASE `aaab`'
CREATE DATABASE aaab
CREATE DATABASE`aaab`
CREATE DATABASE`aaa@@@#b`
CREATE DATABASE`aaa``b`
SQL;

preg_match_all('/CREATE\s+DATABASE\s?((`?)[\w@#]+\2)+/is',$sql,$matchs);
print_r($matchs);
<br /><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
PHP code
<?php $sql=<<<SQL
CREATE DATABASE `aaab`'
CREATE DATABASE aaab
CREATE DATABASE`aaab`
CREATE DATABASE`aaa@@@#b`
CREATE DATABASE`aaa``b`
CREATE DATABASE aaab DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
SQL;

preg_match_all('/CREATE\s+DATABASE\s*(([`\'"]?)([\w@#`]+)\2\s?)/is',$sql,$matchs);
print_r($matchs[3]);
<br /><font color="#e78608">------解决方案--------------------</font><br>名称里面可以出现什么符号 往中括号里面加就行了 比如你说的()  [\w@#`()]<br>我只能保证数据库名两边同时出现 `   或者同时不出现 `,不能控制里面 ` 的数量
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨
就是说只要用上`` 名字里可以有任何符。

没用上`的话 名字只能用字母与数字,而且前面要加个空格与DATABASE文字分隔开。

------解决方案--------------------
那串符号可以用[:punct:]表示,[:punct:]代表标点符号字符 类似的还有[:alnum:] [:space:] ... 各表示几种类型的字符串
可以修改如下
PHP code
preg_match_all('/CREATE\s+DATABASE\s*((([`\'"])?)([\w[:punct:]]+)\2\s?)/is',$sql,$matchs);
<br><font color="#e78608">------解决方案--------------------</font><br>
试下<br>
Copy after login
PHP code
$databases = <font color="#e78608">------解决方案--------------------</font><br>上面的不行,``中间的`必须是偶数位。。这个比较麻烦。
<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨


没错啊。``中间的`是偶数位能匹配出来就没问题。为单数的话说明是有问题的SQL了,不需考虑。

你上面的`aaa````bbb` 匹配不了哦。

引用:

上面的不行,``中间的`必须是偶数位。。这个比较麻烦。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

What is the difference between HQL and SQL in Hibernate framework?

How to solve discuz database error How to solve discuz database error Nov 20, 2023 am 10:10 AM

How to solve discuz database error

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S Jun 15, 2024 pm 09:49 PM

Leak reveals key specs of Intel Arrow Lake-U, -H, -HX and -S

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

Usage of division operation in Oracle SQL

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What does the identity attribute in SQL mean?

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Comparison and differences of SQL syntax between Oracle and DB2

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? May 27, 2023 pm 01:10 PM

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations?

See all articles