MySQL regex replace?

前端 未结 3 596
北海茫月
北海茫月 2021-01-19 23:47

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, a

相关标签:
3条回答
  • 2021-01-20 00:12

    Replacing text in MySQL with Regular Expressions

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

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

    Examples:

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

    Yields:

    the quick brown 
    

    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.

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

    0 讨论(0)
  • 2021-01-20 00:18

    There is no regexp replace feature in mySQL proper: The regex functions are match only.

    There seems to be a user defined function that adds the functionality somehow, but it requires re-compiling mySQL and is probably not an option.

    I'd recommend doing this using a programming/scripting language like PHP, using its built-in regex replace functions to change the content, and update the records.

    Edit: overlooked the php tag.

    0 讨论(0)
  • 2021-01-20 00:18

    Html is not a regular language therefore trying to parse it with regex is not the best option. In my opinion i would want to leverage a html parser to do this job. Here is a sample parser.

    Enjoy!

    0 讨论(0)
提交回复
热议问题