Postgresql regexp_replace [英] Postgresql regexp_replace

查看:91
本文介绍了Postgresql regexp_replace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用空格替换 1990-2050 中的任何年份?

我可以按如下方式替换任何 4 位数字

select regexp_replace('sdfg 2000', '(\y(\d{4})\y)', '', 'g');

但是我如何另外检查范围?欢迎任何帮助

解决方案

我发现了一种替代方法来解决您的问题.看看.

您希望替换 1990-2050 中的年份.让我们把这个范围分成

  1. 1990-1999
  2. 2000-2049
  3. 2050

所有三个范围都可以通过以下正则表达式进行匹配.

正则表达式: [1][9][9][0-9]|[2][0][0-4][0-9]|2050

说明:

  • [1][9][9][0-9] 将匹配从 19901999 的年份.

  • [2][0][0-4][0-9] 将匹配从 20002049 的年份.

  • 2050 将匹配 2050 字面

  • | 表示修改.它将检查这三种模式中的任何一种.

Regex101 演示

How can I replace any year from 1990-2050 with a space ?

I can replace any 4 digit number as follows

select regexp_replace('sdfg 2000', '(\y(\d{4})\y)', '', 'g');

But how additionally I can check the range? Any help are welcome

解决方案

I have discovered an alternative way to solve your problem. Take a look.

You wish to replace year from 1990-2050. Let's break that range into

  1. 1990-1999
  2. 2000-2049
  3. 2050

All three ranges can be matched by following regex.

Regex: [1][9][9][0-9]|[2][0][0-4][0-9]|2050

Explanation:

  • [1][9][9][0-9] will match years from 1990 to 1999.

  • [2][0][0-4][0-9] will match years from 2000 to 2049.

  • 2050 will match 2050 literally

  • | means alteration. It will check either of these three patterns.

Regex101 Demo

这篇关于Postgresql regexp_replace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆