如何使javascript regex在postgresql regexp_matches中工作?

时间:2019-03-23 21:43:41

标签: regex postgresql

我要捕获的文档中所有小于30%的百分比(最多4个小数位)。

这是一个有效的javascript正则表达式示例:https://regex101.com/r/iM3nX5/5

当我在Postgres中使用此正则表达式SELECT regexp_matches('11111 11111. 11111.1111 .11111 a111.1111 99 010 101 100 100.01 2.95% 19.5113% 5.32 0.0250 9.32 24.32 0.0023 30.20 29.23', '\b(?:[1-2]?[0-9]\.[0-9]{1,4})\b[^a-zA-Z\d<]{0,3}%?', 'g') 时,它不起作用:

import itertools

day = ["Mon", "Tue", "Wed"]
time = ["7:00", "8:00", "9:00"]
team = ["Lakers", "Warriors", "Kings"]
month = ["Jan", "Feb", "Mar"]
city = ["LA", "SF", "Sac"]

time_filtered = ["8:00", ]
month_filtered = ["Jan", ]

for i, j, k, l, m in itertools.product(
                        day, time_filtered, team, month_filtered, city):
    model += z[i,j,k,l,m] <= 0

我想让它在Postgres中工作的任何想法?

谢谢。

1 个答案:

答案 0 :(得分:1)

字边界是元凶。您需要使用\m / \M来匹配前导/后继单词边界,或者使用\y来等效于\b。参见Table 9.20. Regular Expression Constraint Escapes

  

\m仅在单词开头匹配
  \M仅在单词的末尾匹配
  \y仅在单词的开头或结尾匹配

例如您可以使用

'\m(?:[1-2]?[0-9]\.[0-9]{1,4})\M[^a-zA-Z\d<]{0,3}%?'

'\y(?:[1-2]?[0-9]\.[0-9]{1,4})\y[^a-zA-Z\d<]{0,3}%?'

请参见PostgreSQL demo online。结果:

enter image description here