gpt4 book ai didi

regex - 两种模式之间的 Postgres regexp_matches

转载 作者:行者123 更新时间:2023-11-29 13:11:51 24 4
gpt4 key购买 nike

我正在尝试像 Postgres 9.4 那样拆分表达式:“一些文本 123_good_345 和其他文本 123_some_invalid 和 222_work ok_333 停止。”

使用模式:(\d+\_.*\_\d+\D)+?结果是:

"123_good_345"
"123_some_invalid and 222_work ok_333"

但是我需要

"123_good_345"
"222_work ok_333"

注意,忽略“123_some_invalid”

请帮忙!

最佳答案

你可以使用

\d+_(?:(?!\d_).)*_\d+

参见 regex demo .或者,如果 \d+__\d+ 之间不能有数字,则使用

\d+_\D+_\d+

参见 this regex demo .

详情

  • \d+ - 一位或多位数字-_ - 下划线
  • (?:(?!\d_).)* - 任何字符,0 次或多次重复,尽可能多,不以数字开头 + _ 字符序列
  • \D+ - 除数字以外的任何 1+ 个字符
  • _ - 下划线
  • \d+ - 1+ 位数字。

参见 PostgreSQL demo :

SELECT unnest(regexp_matches('some text 123_good_345 and other text 123_some_invalid and 222_work ok_333 stop.', '\d+_(?:(?!\d_).)*_\d+', 'g'));

SELECT unnest(regexp_matches('some text 123_good_345 and other text 123_some_invalid and 222_work ok_333 stop.', '\d+_\D+_\d+', 'g'));

enter image description here

关于regex - 两种模式之间的 Postgres regexp_matches,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53747424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com