postgresql varchar字段regexp_replace正则替换操作
1.替换目标
1).contact字段类型varchar。
2).去掉字段中连续的两个,每个等号后面数字不同,
effective_caller_id_name=051066824513,effective_caller_id_number=051066824513
2.查询原字段内容
selectcontact frompbx_agents wherecontact~'effective_caller_id_name='limit2 "{sip_append_audio_sdp=a=fmtp:18annexb=no,call_timeout=60,effective_caller_id_name=051066824513,effective_caller_id_number=051066824513,cti_account=9,cti_agent_id=1102441276,cti_account_sid=5be394c3f8754bd89b9618937c687068,absolute_codec_string=\'OPUS,G729(...)" "{sip_append_audio_sdp=a=fmtp:18annexb=no,call_timeout=60,effective_caller_id_name=051066824824,effective_caller_id_number=051066824824,cti_account=9,cti_agent_id=1103211003,cti_account_sid=4f3fae0e71b74bdaa3824e6ec7771815,absolute_codec_string=\'OPUS,G729(...)"
3.查询语句验证替换
selectregexp_replace(contact,'(effective_caller_id_name=\d+,)+?','','g') frompbx_agents wherecontact~'effective_caller_id_name=' "{sip_append_audio_sdp=a=fmtp:18annexb=no,call_timeout=60,effective_caller_id_number=051066824513,cti_account=9,cti_agent_id=1102441276,cti_account_sid=5be394c3f8754bd89b9618937c687068,absolute_codec_string=\'OPUS,G729\'}sofia/gateway/agentProxy/1102441276" "{sip_append_audio_sdp=a=fmtp:18annexb=no,call_timeout=60,effective_caller_id_number=051066824824,cti_account=9,cti_agent_id=1103211003,cti_account_sid=4f3fae0e71b74bdaa3824e6ec7771815,absolute_codec_string=\'OPUS,G729\'}sofia/gateway/agentProxy/1103211003"
4.执行替换操作
updatepbx_agentssetcontact=regexp_replace(contact,'(effective_caller_id_name=\d+,)+?','','g') wherecontact~'effective_caller_id_name=' Queryreturnedsuccessfully:3929rowsaffected,505msecexecutiontime.
5.查询验证替换
selectcontact frompbx_agents "{sip_append_audio_sdp=a=fmtp:18annexb=no,call_timeout=60,cti_account=9,cti_agent_id=1100891004,cti_account_sid=23869df09f2f47f0ae80a7cbc45e5185,absolute_codec_string=\'OPUS,G729\'}sofia/gateway/agentProxy/1100891004" "{sip_append_audio_sdp=a=fmtp:18annexb=no,call_timeout=60,cti_account=9,cti_agent_id=1102081069,cti_account_sid=9f2c1574fcb5497994cb9b892aee0d1c,absolute_codec_string=\'OPUS,G729\'}sofia/gateway/agentProxy/1102081069"
补充:PostgreSql regexp_replace替换括号里面所有字符串
我就废话不多说了,大家还是直接看代码吧~
//PG数据替换数据中的例如(1222-2)中的所有值 selectregexp_replace('在激励(1222-2)','(\(.*\))','','')
以上为个人经验,希望能给大家一个参考,也希望大家多多支持毛票票。如有错误或未考虑完全的地方,望不吝赐教。