如何在 MySQL/正则表达式替换器中计算单词?

时间:2023-04-29
本文介绍了如何在 MySQL/正则表达式替换器中计算单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我如何在 MySQL 查询中具有与 Regex.Replace 函数相同的行为(例如在 .NET/C# 中)?

How can I, in a MySQL query, have the same behaviour as the Regex.Replace function (for instance in .NET/C#)?

我需要它,因为和很多人一样,我想计算一个字段中的单词数.但是,我对以下答案并不满意(在该网站上多次给出):

I need that because, as many people, I would like to count the number of words in a field. However, I'm not satisfied with the following answer (given several times on that site):

SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', '') +1 FROM table

因为当两个单词之间的空格超过一个时,效果不会很好.

Because it doesn't give good results when there are more that one space between two words.

顺便说一句,我认为 Regex.Replace 函数可能很有趣,所以欢迎所有好的想法!

By the way, I think the Regex.Replace function may be interesting so all the good ideas are welcome !

推荐答案

有可用的 REGEXP_REPLACE 作为 MySQL 用户定义功能.

There's REGEXP_REPLACE available as MySQL user-defined functions.

字数统计:如果你能控制进入数据库的数据,你可以在插入前去除双空格.此外,如果您必须经常访问字数统计,您可以在代码中计算一次并将计数存储在数据库中.

Word counting: If you can control the data going into the database, you can remove double whitespace before insert. Also if you have to access the word count often, you can compute it once in your code and store the count in the database.

这篇关于如何在 MySQL/正则表达式替换器中计算单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:ms-access 中有 group_concat 函数吗? 下一篇:mysql全文搜索失败

相关文章