我想将文本返回到的系、学院的左边返回波莫納學院罗格斯大学
我正在努力让它发挥作用,在那里我可以传递多重选项。即在";学校;或";部门;我在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_words1 | before_wwords2 | bever_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
——一个单词边界