MySQL正则表达式替换? [英] MySQL regex replace?

查看:672
本文介绍了MySQL正则表达式替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有斑点文本字段的表.此Blob中包含很多html. html之一是h2.我想在h2标签中找到一个单词的所有实例,然后用另一个单词替换它(同时保持其他单词不变).

I've got a table with blob text fields. This blob has a lot of html in it. One of the pieces of html is an h2. I would like to find all instances of a word in the h2 tag, and replace it with another word (while leaving other words the same).

例如,在以下示例中,我想用"bumbles"替换h2"wiggles":

For example, I would like to replace the h2 "wiggles" with "bumbles" in the following:

之前:

<h2>This is some wiggles html!</h2>
<p>And here is some more wiggles html that could be ignored</p>
<h2>And this is a decoy h2</h2>

之后:

<h2>This is some bumbles html!</h2>
<p>And here is some more wiggles html that could be ignored</p>
<h2>And this is a decoy h2</h2>

我担心的一个问题是,正则表达式不会在第一个h2结束时停止,而是一直持续到最后一个结束.

A pitfall I'm concerned about is the regex not stopping at the end of the first h2, and rather continuing through to the last closing of it.

我可以访问shell和phpmyadmin.

I have access to shell and phpmyadmin.

推荐答案

在MySQL中使用正则表达式替换文本

您可以将库添加到MySQL以获得此功能.

Replacing text in MySQL with Regular Expressions

You can add a library to MySQL to gain this feature.

添加:LIB_MYSQLUDF_PREG
允许:正则表达式搜索和使用PCRE替换.
站点: http://www.mysqludf.org/lib_mysqludf_preg/

Adding: LIB_MYSQLUDF_PREG
Allows: Regular expression search & replace using PCRE.
Site: http://www.mysqludf.org/lib_mysqludf_preg/

SELECT PREG_REPLACE('/(.*?)(fox)/' , 'dog' , 'the quick brown fox' );

收益率:

the quick brown 

使用正则表达式匹配HTML

使用regexp解析HTML并不容易,而且有很多陷阱.但是,您的示例非常简单,您应该能够做自己想做的事情.

Matching HTML with Regular Expressions

Parsing HTML with regexp is not easy and has a lot of pitfalls. However, your example is simple enough that you should be able to do what your looking to do.

我认为这会有所帮助: http://haacked.com/archive /2004/10/25/usingregularexpressionstomatchhtml.aspx

I think this will be helpful: http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx

这篇关于MySQL正则表达式替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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