regex - PostgreSQL regex_replace
问题描述
I want to convert a string ___abc_[_]xyz
to ...abc.\_xyz
using regular expression.
Is it possible to convert _
and [_]
into .
and \_
respectively in the same query?
This is what I have done so far:
SELECT regexp_replace('___abc_[_]xyz','\[(.)\]','\\\1','g');
and
SELECT regexp_replace('___abc_[_]xyz','\[_\]','\_','g');
The Result of both queries is: ___abc_\_xyz
解决方案
双重 regexp_replace 可以完成这项工作:
SELECT regexp_replace(regexp_replace('___abc_[_]xyz','(?!\[)_(?!\])','.','g'),'\[_\]','\\_','g');
regexp_replace
----------------
...abc.\_xyz
(1 row)
第一个, using将用点替换&(?!\[)_(?!\])
之间的下划线 NOT 。[
]
第二个将替换[_]
为\_
推荐阅读
- nlp - 使用 OpenIE 提取给定实体的关系
- python - Python 替代全局变量
- android - Android - 使用 MIDI 文件播放 SoundFont
- multithreading - 提高 scala .par 操作的并行度
- c# - CellStyle 的 NPOI 字体不起作用,必须修复工作表
- r - 如何加快 bookdown 的生成?
- c# - 统一增加游戏对象的规模不起作用?
- c# - 以下方法或属性之间的调用不明确:'MvvmCross.Binding.BindingContext.MvxBindingContextOwnerExtensions
- python-3.x - 无法使用 sympy 获取简化坐标 - python
- xamarin.ios - “内存”类型
' 存在于 'System.Memory 4.0.1.0 和 mscorlib 2.0.5.0