简体   繁体   English

substring_index中的Mysql多个加工模式

[英]Mysql multiple maching patterns in substring_index

Can I use something like case to give multiple matching patterns in substring_index ? 我可以使用case东西在substring_index给出多个匹配模式吗?

More specifically in my case, can I matching a set of chars according to their ascii? 更具体地说,在我的情况下,我可以根据他们的ascii匹配一组字符吗?

Add some examples: 添加一些示例:

中文Q100
中文T800
中文中文K999

The strings start with some Chinese characters, then following by some numbers or latin letters, what I want is to split the string into two parts: one contains the Chinese characters(from leftmost to the first western letter), the other is from the first western letter to the rightmost. 字符串以一些汉字开头,然后是一些数字或拉丁字母,我想要的是将字符串分成两部分:一部分包含汉字(从最左边到第一个西部字母),另一部分来自第一部分最西边的西方信件。

Like these: 像这些:

中文, Q100
中文, T800
中文中文, K999

There are multiple ways to resolve a matter. 有多种方法可以解决问题。 I'll give you 3 of them, starting from most right. 从最右边开始,我会给你3个。

Architecture solution 架构解决方案

Using application 使用应用程序

Your question is about - replacing by regular expression. 你的问题是关于 - 用正则表达式替换。 And that has a weak support in MySQL (to say precisely, there's no support for replacing by regex). 而且MySQL的支持很弱(准确地说,没有人支持用正则表达式替换)。 Thus, you may do: select whole record, then split it in applicaition, using a-zA-Z0-9 mask, for example. 因此,您可以这样做:选择整个记录,然后使用a-zA-Z0-9掩码将其拆分为应用程序。

Or may be change table structure? 或者可能是改变表结构?

Well, alternative is: may be you should just separate this data to 2 columns? 那么,替代方案是:您可能应该将此数据分成2列吗? If your intention is to work with separate parts of data, then may be it's a sign to change your DB architecture? 如果您打算使用单独的数据部分,那么可能是更改数据库体系结构的标志吗?


Using MySQL 使用MySQL

Second way is to use MySQL. 第二种方式是使用MySQL。 To do it - yes, you'll use REPLACE() as it is. 要做到这一点 - 是的,你将使用REPLACE() For instance, to get rid of all alphanumeric symbols, you'll do: 例如,要删除所有字母数字符号,您将执行以下操作:

SELECT [...REPLACE(REPLACE(str, 'z', ''), 'y', '')...]

that is a pseudo-SQL, since posting whole 26+26+10 instances of REPLACE would be mad (however, using this is also mad). 这是一个伪SQL,因为发布整个26 + 26 + 10个REPLACE实例会很疯狂(但是,使用它也很疯狂)。 But that will resolve your issue, of course. 但这当然会解决你的问题。


Using external REGEXP solution 使用外部REGEXP解决方案

This is third way and it has two subcases. 这是第三种方式,它有两个子类。 You may either use UDF or stored routines. 您可以使用UDF或存储的例程。

Using UDF 使用UDF

There are third-party libraries which provide regular expression replacement functionality. 有第三方库提供正则表达式替换功能。 Then all you need to do is to include those libraries into your server build. 然后,您需要做的就是将这些库包含到您的服务器构建中。 Example: lib_mysqludf_preg This, however, will require additional actions to use those libraries. 示例: lib_mysqludf_preg但是,这将需要其他操作才能使用这些库。

Using stored routines 使用存储的例程

Well, you can use stored routines to create your own replacement function. 那么,您可以使用存储的例程来创建自己的替换函数。 Actually, I have already written such library, it's called mysql-regexp and it provides REGEXP_REPLACE() function, which allows you to do replacements in strings by regular expressions. 实际上,我已经编写了这样的库,它叫做mysql-regexp ,它提供了REGEXP_REPLACE()函数,它允许你用正则表达式替换字符串。 It's not well-tested, so if you'll decide to use it - do that on your own risk. 它没有经过充分测试,所以如果您决定使用它 - 请自行承担风险。 Sample would be: 样本将是:

mysql> SELECT REGEXP_REPLACE('foo bar34 b103az 98feo', '[^a-z]', '');
+--------------------------------------------------------+
| REGEXP_REPLACE('foo bar34 b103az 98feo', '[^a-z]', '') |
+--------------------------------------------------------+
| foobarbazfeo                                           |
+--------------------------------------------------------+
1 row in set (0.00 sec)

Since it's completely written with stored code, you won't need to re-build your server or whatever. 由于它完全使用存储代码编写,因此您无需重新构建服务器或其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM