dsgixl5195 2013-08-27 08:01
浏览 42
已采纳

在mysql中使用REGEX对特定字符进行严格的数字检查

In my db data are stored like below:

Q_101_B1
Q_101_B2
Q_101_B17
Q_101_Bch1
Q_101_Ben1
Q_101_B238
Q_101_B

I have to pull only those data which has digits strictly after char 'B', thus in this case it should pull all the data except these two Q_101_Bch1, Q_101_Ben1.

I have tried with this query:

select lang from test_table where lang REGEXP  '^[Q_101_B]|[0-9]'

But unfortunately its pulling all of them.

What should be the exact regex for this query?

Note: Q_101_B can be a different string which will come dynamically, and I will append this while building the query inside my PHP code.

  • 写回答

1条回答 默认 最新

  • douzhi8488 2013-08-27 08:02
    关注

    Your original pattern would match any string beginning with the characters Q, _, 1, 0, or B or containing a decimal digit.

    Try using this pattern instead:

    select lang from test_table where lang REGEXP  '^Q_101_B[0-9]'
    

    This will match any string beginning with Q_101_B followed by a decimal digit.

    However, since you indicated you want Q_101_B to be returned as well (with no decimal), you might use this:

    select lang from test_table where lang REGEXP  '^Q_101_B([0-9]|$)'
    

    This will match any string beginning with Q_101_B followed by either a decimal digit or the end of the string.

    You can see a demonstration of this pattern here.

    Also, since you stated you that Q_101_B is just a sample string, and may be replaced by a different string at run-time, I'd recommend you look at using the preg_quote method to safely escape the literal within your pattern.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥30 怎么把PCK、OKS指标添加到yolov11中
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 求GCMS辅导数据分析
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部