gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
怎样在oracle 里用regular expression提取特定字段
- User
怎样在oracle 里用regular expression提取特定字段
- Assistant
在Oracle中,可以使用REGEXP_SUBSTR函数来提取特定字段。该函数需要三个参数:输入字符串、正则表达式以及开始位置。
例如,假设我们有以下字符串:
'John Smith (31 years old), Mary Johnson (24 years old), Peter Lee (29 years old)'我们想提取所有人的年龄。我们可以使用以下正则表达式:
\d+ years old该正则表达式可以匹配一个或多个数字,后跟“years old”字样。
我们可以使用以下SQL语句来提取所有人的年龄:
SELECT REGEXP_SUBSTR('John Smith (31 years old), Mary Johnson (24 years old), Peter Lee (29 years old)', '\d+ years old', 1, LEVEL) AS age FROM dual CONNECT BY REGEXP_SUBSTR('John Smith (31 years old), Mary Johnson (24 years old), Peter Lee (29 years old)', '\d+ years old', 1, LEVEL) IS NOT NULL;该SQL语句使用CONNECT BY子句和LEVEL伪列来逐一提取匹配项。最终输出如下:
AGE ---------- 31 years old 24 years old 29 years old如果只想提取年龄数字,可以使用以下SQL语句:
SELECT REGEXP_SUBSTR('John Smith (31 years old), Mary Johnson (24 years old), Peter Lee (29 years old)', '\d+', 1, LEVEL) AS age FROM dual CONNECT BY REGEXP_SUBSTR('John Smith (31 years old), Mary Johnson (24 years old), Peter Lee (29 years old)', '\d+', 1, LEVEL) IS NOT NULL;该SQL语句使用相同的CONNECT BY子句和LEVEL伪列,但使用“\d+”正则表达式来匹配一个或多个数字。最终输出如下:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.