如何使用bigquery regexp_extract提取一个或多个关键字左侧的句子部分



我想将文本返回到的系、学院的左边返回波莫納學院罗格斯大学

我正在努力让它发挥作用,在那里我可以传递多重选项。即在";学校;或";部门;我在before_words_OR行中尝试了|OR运算符,但我做错了。

regexp_extract(institute_name,r'^(.*)department of|school of',1,1)

如果不存在";学校;或";部门;在institute_name中,它应该返回institute_name

with t1 as 
(
select 'pomona college department of chemistry' institute_name,
union all select 'rutgers college school of engineering chemical engineering' 
)
select 
regexp_extract(institute_name,r'^(.*)department of',1,1) before_words1,
regexp_extract(institute_name,r'^(.*)school of',1,1) before_words2,
regexp_extract(institute_name,r'^(.*)department of|school of',1,1) before_words_or
from t1;

实际结果

before_words1before_wwords2bever_words_或
pomona学院
罗格斯学院

您可以使用

r'^(.*?)s*(?:department|school)s+of'

或者,使用单词边界来确保of作为一个完整的单词匹配:

r'^(.*?)s*(?:department|school)s+ofb'

请参阅regex演示。

详细信息

  • ^-字符串的开头
  • (.*?)-捕获组:除了换行字符之外的任何零个或多个字符,尽可能少
  • s*-零个或多个空白
  • (?:department|school)-与department或(|(school匹配的非捕获组
  • s+-一个或多个空白
  • of-一个of
  • b——一个单词边界

最新更新