sql中常用的正则匹配

sql的正则匹配


语法:

regexp ‘表达式’

1.直接匹配

字符中能匹配到就返回1,不能匹配到就返回0

> select 'hello' regexp 'he'
> 1
> select 'hello' regexp 'llo'
> 1
> select 'hello' regexp 'hee'
> 0

2.符号’^’

代表开头,需要开头是某个字符

> select 'hello' regexp '^he'
> 1
> select 'hello' regexp '^llo'
> 0

3.符号’$’

代表结尾,需要结尾是某个字符

> select 'hello' regexp 'llo$'
> 1
> select 'hello' regexp 'll$'
> 0

4.符号’.’

代表任意一个字符,可以匹配上任何一个字符

> select 'hello' regexp '.ello'
> 1
> select 'hello' regexp '.lo'
> 1
> select 'hello' regexp '.c'
> 0

5.符号’+’

前面的字符至少出现1次

> select 'hello' regexp 'h+'
> 1
> select 'hello' regexp 'o+'
> 1
> select 'hello' regexp 'c+'
> 0

6.符号’*’

前面的数字至少出现0次

> select 'hello' regexp 'he*'
> 1
> select 'hello' regexp 'c*'
> 1
> select 'hello' regexp 'c*e'
> 1
> select 'hello' regexp 'c*c'
> 0

7.符号’?’

前面的数字最多出现1次

> select 'hello' regexp 'c?c?h'
> 1
> select 'hello' regexp 'h?ello'
> 1
> select 'hello' regexp 'c?c'
> 0

8.符号’()’

代表一个整体,全体匹配

> select 'hello' regexp '(cda)?he'
> 1
> select 'hello' regexp '(helc).o'
> 0

9.符号’[]’

匹配符号内的任何一个字符即可

> select 'hello' regexp '[hege]ello'
> 1
> select 'hello' regexp '[jilsdf]hello'
> 0

注意:

如果想要匹配字符’[’ 或者’]‘,需要把’[‘放在’]‘前,匹配’]'也是同理

> select '[[]]' regexp '[asd[]'
> 1
> select '[[]]' regexp '[[asd]'
> 0
> select '[[]]' regexp '[]asd'
> 1
> select '[[]]' regexp '[asd]]'
> 0

符号[]中可以使用’-'表示区间,表示匹配区间内的数据

> select 'hello' regexp '[a-z]'
> 1

如果匹配’-', 需要放到[]的两端

> select 'asg-' regexp '[-ccc]'
> 1
> select 'asg-' regexp '[cc-c]'
> 0

如果是[^],表示不含[]里的任何字符

> select 'hello' regexp '[^0-9]'
> 1
> select 'hello' regexp '[^a-z]'
> 0

10.符号’|’

匹配分割的任何一个字符

> select 'hello' regexp 'h|c'
> 1
> select 'hello' regexp 'c|g'
> 0

11.符号’{t}’

匹配前面的字符t次

> select 'hello' regexp 'l{2}'
> 1
> select 'hello' regexp 'l{3}'
> 0

符号{t,s}

匹配次数在t-s次均可

> select 'hello' regexp 'l{1,3}'
> 1

举例:

  1. 以app、ios、androd开头的字符串

    > select 'tmma.ctime' regexp '^app|^ios|^androd'
    > 0
    > select 'appa.ctime' regexp '^app|^ios|^androd'
    > 1
    
  2. 以tm或者以re开头,并且以me结尾的字符串

    > select 'tmma.ctime' regexp '^tm.*me$|^re.*me$'
    > 1
    > select 'tmma.ctim' regexp '^tm.*me$|^re.*me$'
    > 0
    
### 如何在 SQL 中使用正则表达式匹配中文字符 在 SQL 数据库中,不同数据库管理系统 (DBMS) 对于正则表达式的支持有所不同。对于 MySQL 和 PostgreSQL 这样的 DBMS,在查询语句中可以利用特定函数来实现对字符串模式的匹配操作。 #### 使用 `REGEXP` 或者 `RLIKE` MySQL 支持通过 `REGEXP` 或者同义词 `RLIKE` 来执行复杂的模式匹配: - 要匹配任何单个汉字,可采用如下方式构建正则表达式 `[一-龥]`[^1]。 ```sql SELECT * FROM table_name WHERE column_name REGEXP '[一-龥]'; ``` 此命令会返回表 `table_name` 中列 `column_name` 包含任意一个CJK统一汉字记录的结果集。 为了更精确地处理多字节字符集中的文字,比如全角符号或者其他亚洲语言的文字,应当考虑整个 Unicode CJK 统一表意文字范围 U+4E00 到 U+9FFF: ```sql SELECT * FROM table_name WHERE column_name REGEXP '\\u4e00-\\u9fff'; ``` 请注意,具体转义序列取决于所使用的编程环境以及客户端工具配置情况。 #### 处理编码问题 当涉及到非 ASCII 字符时,确保数据库连接设置正确指定了字符集是非常重要的。如果遇到乱码或其他编码相关的问题,则可能需要调整 PHP 程序里的编码转换逻辑,例如将 UTF-8 编码的数据转化为 Windows-1251 编码再存入数据库[^2]。 然而,通常推荐保持一致性的字符编码方案——即在整个应用程序栈内都使用 UTF-8 ——这有助于减少潜在麻烦并提高跨平台兼容性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牧码文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值