postgresql - postgres regexp_replace 否定连续字符组的存在

标签 postgresql regex-negation regexp-replace

我有一个数据库导出,其中包含一些错误的字符替换(如 è => e')。所以我必须在 postgres 中将其改回。
我要疯狂地制作一个能捕捉到这样的东西的多合一正则表达式:

cassine' de' pecche' e'

它必须成为

cassinè de' pecchè è

(de'不得改变)。

我设法通过两次通过:

UPDATE mytable SET comun1=UPDATE mytable SET comun1=regexp_replace(column1,'([^dnNn])(e\'')', '\1\2è', 'g');   

UPDATE mytable SET comun1=UPDATE mytable SET comun1=regexp_replace(column1,'([^\s])([dnNn])(e\'')', '\1\2è', 'g'); 

基本上我想从替换空格后跟 d 或 n 和后跟 e'(如“de'”)中排除,并在所有其他情况下更改 e'。
我尝试了 (?!\s[nNdD])(e\'') 但它仍然将“de'”更改为“dè”

有人对此有解决方案吗?

最佳答案

select regexp_replace($$cassine' de' pecche' e'$$, $$(\s[^dn]?|\w\w)e'$$, '\1è', 'gi')
    regexp_replace    
----------------------
 cassinè de' pecchè è
(1 row)

解释:

(\s[^dn]?|\w\w)e'
 ^       ^     ^ 
 |       |     followed by e'
 |       or 2 word chars
 space optionally followed by d or n

关于postgresql - postgres regexp_replace 否定连续字符组的存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28459540/

相关文章:

node.js - 如何使用 sequelize.js 保存从 FineOne 返回的关联?

sql - 带通配符的慢连接查询

sql - 删除主键列后如何重新创建主键?

regex - PostgreSQL 函数中嵌套正则表达式替换的替代方案?

python - 仅替换文件中多次出现的匹配组

postgresql - CASE WHEN 条件 1 AND 条件 2 then x else y postgreSQL

正则表达式仅在缺少给定子字符串/后缀的情况下匹配整个字符串

php - 正则表达式中带\b 的美元符号包含额外空间

php - 有没有办法在 PHP 正则表达式中指定 "any character but [aeiou]"?

Postgresql:删除某些类型的数字之间的空格