postgresql - Postgres 正则表达式停止创建双倍空间
问题描述
我有一个看起来像这样的查询:
select regexp_replace('john (junior) jones','\([^)]*\)','','g');
regexp_replace
------------------
john jones
如您所见,此查询删除了括号中的值,但会导致剩余的双倍空格。
有没有简单的方法解决这个问题?
到目前为止,我有这个,它在一定程度上有效:
select regexp_replace((regexp_replace('john (junior) jones','\([^)]*\)','','g')),'\s','');
regexp_replace
------------------
john jones
上面的工作但不是当我通过这样的事情时:
select regexp_replace((regexp_replace('john (junior) jones (hughes) smith','\([^)]*\)','','g')),'\s','');
regexp_replace
---------------------
john jones smith
解决方案
SELECT regexp_replace(
'john (junior) jones (hughes) smith',
' *\([^)]*\) *',
' ',
'g'
);
regexp_replace
══════════════════
john jones smith
(1 row)
解释正则表达式:
- 任意数量的空格,后跟左括号 (
*\(
) - 不是右括号 (
[^)]*
)的任意数量的字符 - 右括号和任意多个空格 (
\) *
)
那被一个空格代替。
推荐阅读
- javascript - 如何使用 useEffect 取消选中
- javascript - 嵌入多个 Youtube 直播并使用按钮管理显示
- python - Python:用 scipy different_evolution 拟合参数,如何强制一个参数小于另一个参数?
- linux - 如果每行包含时间戳,如何使用 bash 获取日志记录频率?
- spring - ProxyingHandlerMethodArgumentResolver 干扰数据绑定
- python - 如何告诉 pip 并行化编译
- python - 创建反向虚拟变量
- .net - 为什么我们在 ASP.NET Core Web Application 中有一个 Main() 方法(入口方法)?这种方法背后的原因是什么?
- sql - 统计一个表是否满足一个或多个条件 SQL
- typescript - 在 Puppeteer 中使用向下箭头滚动