本文介绍了MySQL 子串匹配使用正则表达式;子字符串包含“男人"而不是“女人"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用正则表达式从数据库中获取数据时遇到问题.当我在标签中搜索男人"时,它返回的标签也包含女人";因为它的子串.

SELECT '#hellowomanclothing' REGEXP '^(.)*[^wo]man(.)*$';# 正确返回 0,它包含 'woman'SELECT '#helloowmanclothing' REGEXP '^(.)*[^wo]man(.)*$';# 返回 0 不正确,它可以包含除 'woman' 以外的任何内容SELECT '#stylemanclothing' REGEXP '^(.)*[^wo]man(.)*$';# 正确返回 1

如何更新正则表达式,当我搜索男人"时,它应该只返回包含男人"而不是女人"的标签?

解决方案

n-dru 模式的一种变体,因为你不需要描述所有的字符串:

SELECT '#hellowomanclothing' REGEXP '(^#.|[^o]|[^w]o)man';

注意:如果标签包含男人"和女人",则此模式将返回 1.如果您不想要 Gordon Linoff 解决方案正是您要找的.

I have an issue while I fetch data from database using regular expression. While I search for 'man' in tags it returns tags contains 'woman' too; because its substring.

SELECT '#hellowomanclothing' REGEXP '^(.)*[^wo]man(.)*$'; # returns 0 correct, it contains 'woman'
SELECT '#helloowmanclothing' REGEXP '^(.)*[^wo]man(.)*$'; # returns 0 incorrect, it can contain anything other than 'woman' 
SELECT '#stylemanclothing' REGEXP '^(.)*[^wo]man(.)*$'; # returns 1 correct

How can I update the regular expression, when I search for 'man' it should return only the tag contains 'man' not 'woman'?

解决方案

A variant of n-dru pattern since you don't need to describe all the string:

SELECT '#hellowomanclothing' REGEXP '(^#.|[^o]|[^w]o)man';

Note: if a tag contains 'man' and 'woman' this pattern will return 1. If you don't want that Gordon Linoff solution is what you are looking for.

这篇关于MySQL 子串匹配使用正则表达式;子字符串包含“男人"而不是“女人"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 19:16