我一直在试着解决这个问题,但我只能做到一半。
想象一下这样的字符串(地址)
White Lund Industrial Estate Unit 11a Southgate
White Lund Industrial Estate Suite 124 Southgate
White Lund Industrial Estate flat A Southgate我想成为
White Lund Industrial Estate Southgate
White Lund Industrial Estate Southgate
White Lund Industrial Estate Southgate有一个模式,如果Unit、Flat、Suite出现在字符串中,则删除它们和后面的单词。
我在postgres做这件事,到目前为止我做到了以下几点:
select REGEXP_REPLACE(lower('White Lund Industrial Estate Unit 11 a Southgate'), 'unit\S*', '');这给了我:
white lund industrial estate 11 a southgate
我如何告诉regex表达式也删除它后面的单词?
谢谢!
发布于 2020-09-22 08:30:29
您可以使用
\s*\y(unit|flat|suite)\s+\S+此外,您还可以使用嵌入式标志选项(?i)使其不区分大小写。
(?i)\s*\y(unit|flat|suite)\s+\S+详细信息
(?i) -嵌入式选项使匹配的大小写不敏感(参见https://www.postgresql.org/docs/current/functions-matching.html)\s* -0或更多空格字符\y -字边界(unit|flat|suite) - unit、flat或suite子字符串\s+ -1或更多空格字符\S+ -1或更多非空白字符。select REGEXP_REPLACE(lower('White Lund Industrial Estate Unit 11a Southgate'), '\s*\y(unit|flat|suite)\s+\S+', '');或,
select REGEXP_REPLACE('White Lund Industrial Estate Unit 11a Southgate', '(?i)\s*\y(unit|flat|suite)\s+\S+', '');输出:

https://stackoverflow.com/questions/64005838
复制相似问题