简体   繁体   English

替换数据库文本字段中出现的所有子字符串

[英]Replace all occurrences of a substring in a database text field

I have a database that has around 10k records and some of them contain HTML characters which I would like to replace. 我有一个大约有10k记录的数据库,其中一些包含我想要替换的HTML字符。

For example I can find all occurrences: 例如,我可以找到所有出现的事件:

SELECT * FROM TABLE
WHERE TEXTFIELD LIKE '%&#47%'

the original string example: 原始字符串示例:

this is the cool mega string that contains &#47

how to replace all &#47 with / ? 如何用/ ?替换所有&#47

The end result should be: 最终结果应该是:

this is the cool mega string that contains /

If you want to replace a specific string with another string or transformation of that string, you could use the "replace" function in postgresql. 如果要将特定字符串替换为另一个字符串或转换该字符串,可以在postgresql中使用“替换”函数。 For instance, to replace all occurances of "cat" with "dog" in the column "myfield", you would do: 例如,要在“myfield”列中用“dog”替换所有出现的“cat”,你会这样做:

UPDATE tablename
SET myfield = replace(myfield,"cat", "dog")

You could add a WHERE clause or any other logic as you see fit. 您可以根据需要添加WHERE子句或任何其他逻辑。

Alternatively, if you are trying to convert HTML entities, ASCII characters, or between various encoding schemes, postgre has functions for that as well. 或者,如果您尝试转换HTML实体,ASCII字符或各种编码方案之间,postgre也具有此功能。 Postgresql String Functions . Postgresql字符串函数

The answer given by @davesnitty will work, but you need to think very carefully about whether the text pattern you're replacing could appear embedded in a longer pattern you don't want to modify. @davesnitty给出的答案可行,但您需要仔细考虑您要替换的文本模式是否嵌入在您不想修改的较长模式中。 Otherwise you'll find someone's nooking a fire , and that's just weird. 否则你会发现有人在点火 ,这很奇怪。

If possible, use a suitable dedicated tool for what you're un-escaping. 如果可能,请使用合适的专用工具来解决您的问题。 Got URLEncoded text? 有URLEncoded文字? use a url decoder. 使用网址解码器。 Got XML entities? 有XML实体吗? Process them though an XSLT stylesheet in text mode output. 通过文本模式输出中的XSLT样式表处理它们。 etc. These are usually safer for your data than hacking it with find-and-replace, in that find and replace often has unfortunate side effects if not applied very carefully, as noted above. 对于您的数据而言,这些通常比使用查找和替换进行黑客攻击更安全,因为如果不仔细应用,查找和替换通常会产生不幸的副作用,如上所述。

It's possible you may want to use a regular expression . 你可能想要使用正则表达式 They are not a universal solution to all problems but are really handy for some jobs. 它们不是解决所有问题的通用解决方案,但对于某些工作来说非常方便。

If you want to unconditionally replace all instances of "&#47" with "/", you don't need a regexp. 如果要无条件地将所有“&#47”实例替换为“/”,则不需要正则表达式。

If you want to replace "&#47" but not "&#471", you might need a regexp, because you can do things like match only whole words, match various patterns, specify min/max runs of digits, etc. 如果你想要替换“&#47”而不是“&#471”,你可能需要一个正则表达式,因为你可以做一些事情,比如只匹配整个单词,匹配各种模式,指定最小/最大数字运行等。

In the PostgreSQL string functions and operators documentation you'll find the regexp_replace function, which will let you apply a regexp during an UPDATE statement. PostgreSQL字符串函数和运算符文档中,您将找到regexp_replace函数,它将允许您在UPDATE语句期间应用正则表达式。

To be able to say much more I'd need to know what your real data is and what you're really trying to do. 为了能够说得更多,我需要知道你的真实数据是什么以及你真正想要做什么。

If you don't have postgres, you can export all database to a sql file, replace your string with a text editor and delete your db on your host, and re-import your new db 如果你没有postgres,你可以将所有数据库导出到sql文件,用文本编辑器替换你的字符串并删除主机上的数据库,然后重新导入你的新数据库

PS: be careful PS:小心点

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

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