如何在MySQL/正则表达式替换器中计算单词数? [英] How to count words in MySQL / regular expression replacer?

查看:77
本文介绍了如何在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/正则表达式替换器中计算单词数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆