duanlv1366 2018-06-01 12:55
浏览 44
已采纳

SQL搜索字符串中的#字符问题

I use some Regexp in my mySQL search queries and it seems to work fine unless I have a # character in the query.

The Regexp matches based on word boundaries because the field in which this query is searching is entire resumes / curriculum-vitaes stored in the SQL database.

For instance this works as expected and returns the correct number of results:

SELECT COUNT(*) n FROM candidate c WHERE (c.CV REGEXP '[[:<:]]java[[:>:]]');

However, this doesn't and returns 0 results when it should return a few hundred:

SELECT COUNT(*) n FROM candidate c WHERE (c.CV REGEXP '[[:<:]]c#[[:>:]]');

I understand now that this is because I am matching based on word boundaries and # cannot be the end of the word. Interestingly, "C++" works fine though.

Is there a way of modifying this REGEXP so it also works with a string like "c#"?

  • 写回答

5条回答 默认 最新

  • douke1905 2018-09-04 15:22
    关注

    You might be able to use something like this:

    SELECT 'c#' REGEXP '(^|[^a-zA-Z0-9_])c#($|[^a-zA-Z0-9_])'
    SELECT 'java' REGEXP '(^|[^a-zA-Z0-9_])java($|[^a-zA-Z0-9_])'
    

    In newer MySQL versions (8.0.4+) which support regex through ICU instead of Henry Spencer's implementation you can use \w which looks a bit cleaner:

    SELECT 'c#' REGEXP '(^|[^\w])c#($|[^\w])'
    SELECT 'java' REGEXP '(^|[^\w])java($|[^\w])'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100