gpt4 book ai didi

sql - BigQuery - 用于匹配已知字符串后的模式的正则表达式(正后向替代)

转载 作者:行者123 更新时间:2023-12-02 16:52:13 25 4
gpt4 key购买 nike

我需要在已知字符串后提取 8 位数字:

| MyString                     | Extract: | 
| ---------------------------- | -------- |
| mypasswordis 12345678 | 12345678 |
| # mypasswordis 12345678 | 12345678 |
| foobar mypasswordis 12345678 | 12345678 |

我可以用正则表达式来做到这一点:

(?<=mypasswordis.*)[0-9]{8})

但是,当我想在 BigQuery 中使用 REGEXP_EXTRACT 命令执行此操作时,我收到错误消息“无法解析正则表达式:无效的 perl 运算符:(?<”。

我搜索了 re2 library,发现其中似乎没有正向回顾的等价物。

有什么办法可以使用其他方法来做到这一点吗?有点像

SELECT REGEXP_EXTRACT(MyString, r"(?<=mypasswordis.*)[0-9]{8}"))

最佳答案

这里需要一个捕获组来提取模式的一部分,请参阅 REGEXP_EXTRACT docs you linked to :

If the regular expression contains a capturing group, the function returns the substring that is matched by that capturing group. If the expression does not contain a capturing group, the function returns the entire matching substring.

另外,.* pattern 成本太高,你只需要匹配单词和数字之间的空格。

一般,“转换”一个(?<=mypasswordis).*具有正面后视的模式,您可以使用 mypasswordis(.*) .

在这种情况下,您可以使用

SELECT REGEXP_EXTRACT(MyString, r"mypasswordis\s*([0-9]{8})"))

或者只是

SELECT REGEXP_EXTRACT(MyString, r"mypasswordis\s*([0-9]+)"))

参见 re2 regex online test .

关于sql - BigQuery - 用于匹配已知字符串后的模式的正则表达式(正后向替代),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58093941/

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