You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle正则表达式:为格式不规范的地址添加空格的问题

Fixing Address Formatting: Avoiding Unwanted Splits in "AVENUE"

I see the issue with your current regex—it's incorrectly splitting "AVENUE" into "AVE NUE" because it's matching the initial "AVE" even when it's part of the full word. Let's fix this by prioritizing full matches first, then handling abbreviations, and finally tackling the "OF" split.

Approach Breakdown

The key is to:

  1. First handle the full "AVENUE" prefix to avoid splitting it.
  2. Then match the "AVE" abbreviation only when it's not the start of "AVENUE".
  3. Finally, split out the "OF" segment when it's embedded in the address.

Working SQL Solution

Here's a tested query that covers all your example cases, with options for both abbreviated ("AVE") and full ("AVENUE") output:

WITH test_addresses AS (
    SELECT 'AVEX' AS raw_address FROM dual UNION ALL
    SELECT 'AVE X' FROM dual UNION ALL
    SELECT 'AVENUEX' FROM dual UNION ALL
    SELECT 'AVENUE X' FROM dual UNION ALL
    SELECT 'AVEOFCITY' FROM dual UNION ALL
    SELECT 'AVENUEOFCITY' FROM dual
)
SELECT
    raw_address,
    -- Option 1: Keep "AVE" abbreviation where applicable
    REGEXP_REPLACE(
        REGEXP_REPLACE(
            REGEXP_REPLACE(raw_address, '^(AVENUE)(\w)', '\1 \2'),
            '^(AVE)(?!NUE)(\w)', '\1 \2'
        ),
        '(\w)(OF)(\w)', '\1 \2 \3'
    ) AS formatted_abbreviated,
    -- Option 2: Expand "AVE" to "AVENUE" consistently
    REGEXP_REPLACE(
        REGEXP_REPLACE(
            REGEXP_REPLACE(raw_address, '^(AVENUE)(\w)', '\1 \2'),
            '^(AVE)(?!NUE)(\w)', 'AVENUE \2'
        ),
        '(\w)(OF)(\w)', '\1 \2 \3'
    ) AS formatted_full
FROM test_addresses;

What Each Regex Does

  1. First Replace: '^(AVENUE)(\w)', '\1 \2'
    Matches addresses starting with "AVENUE" followed by a character, adding a space between "AVENUE" and the next character. This ensures "AVENUEX" becomes "AVENUE X" instead of getting split.

  2. Second Replace: '^(AVE)(?!NUE)(\w)', '\1 \2'
    Uses a negative lookahead (?!NUE) to only match "AVE" when it's NOT followed by "NUE" (so it won't touch "AVENUE"). This turns "AVEX" into "AVE X".

  3. Third Replace: '(\w)(OF)(\w)', '\1 \2 \3'
    Finds "OF" embedded between other characters and adds spaces around it, turning "AVEOFCITY" into "AVE OF CITY".

Test Results

Raw AddressFormatted AbbreviatedFormatted Full
AVEXAVE XAVENUE X
AVE XAVE XAVENUE X
AVENUEXAVENUE XAVENUE X
AVENUE XAVENUE XAVENUE X
AVEOFCITYAVE OF CITYAVENUE OF CITY
AVENUEOFCITYAVENUE OF CITYAVENUE OF CITY

This approach avoids the unwanted split of "AVENUE" and covers all your specified cases. If you need to add more address abbreviations (like "ST" → "STREET"), you can extend this pattern by adding similar priority-based replacements.

内容的提问来源于stack exchange,提问作者Rajiv A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:17:48