【问题标题】:problem with special characters in regexp funcion正则表达式函数中的特殊字符问题
【发布时间】:2021-03-07 09:57:47
【问题描述】:

我在 Oracle 中解析字符串,但提供者的名称有一个特殊字符,如“&”和“-”。我无法处理它。谁能帮帮我?

SELECT arraylist.* , inserted.*
FROM (
    select trim(regexp_substr(str,'[^;]+', 1, level)) as str1
    from ( 
       SELECT ('Cogent Communications Poland Sp. z.o.o. - 100000Mbps;E-point - 100000Mbps; T-Mobile - 100000Mbps; Net  Friends - 100000Mbps' ) as Str 
       FROM dual
       
    
    )
    connect by regexp_substr(str, '[^;]+', 1, level) is not null
) inserted
CROSS APPLY(
    select trim(regexp_substr(str1,'[^-]+', 1, 1)) as key,
           trim(regexp_substr(str1,'[^-]+', 1, 2)) as value
    from dual
) arraylist

结果是:

【问题讨论】:

    标签: oracle regexp-replace regexp-substr


    【解决方案1】:

    这是处理多字符分隔字符串的一种方法。首先 CTE 只是设置数据。 tbl_case 是在分号上拆分的结果。最后拆分 ' - ' 的分隔符以获得您的名称-值对。

    注意几件事。这些值是不现实的,但用于区分不同的行。 '(.*?)(;|$)' 的正则表达式形式正确处理 NULL 元素,如果它们发生的话。有关更多信息,请参阅this post

    -- Set up data
    WITH tbl(ID, DATA) AS (
      SELECT 1, 'Cogent Communications Poland Sp. z.o.o. - 100001Mbps;E-point - 100002Mbps; T-Mobile - 100003Mbps; Net  Friends - 100004Mbps' FROM dual UNION ALL
      SELECT 2, 'Cogent Communications Poland Sp. z.o.o. - 200001Mbps;E-point - 200002Mbps; T-Mobile - 200003Mbps; Net  Friends - 200004Mbps' FROM dual
    ),
    -- Split on semi-colons
    tbl_case(ID, CASE) AS (
    SELECT ID,
           TRIM(REGEXP_SUBSTR(DATA, '(.*?)(;|$)', 1, LEVEL, NULL, 1)) CASE
    FROM tbl
    CONNECT BY REGEXP_SUBSTR(DATA, '(.*?)(;|$)', 1, LEVEL) IS NOT NULL 
      AND PRIOR ID = ID
      AND PRIOR SYS_GUID() IS NOT NULL        
    )  
    --select * from tbl_case;         
    -- Parse cases into name/value pairs
    SELECT ID,
           REGEXP_REPLACE(CASE, '^(.*) - .*', '\1') name,
           REGEXP_REPLACE(case, '.* - (.*)$', '\1') value
    from tbl_case
    
    
    
            ID NAME                                     VALUE               
    ---------- ---------------------------------------- --------------------
             1 Cogent Communications Poland Sp. z.o.o.  100001Mbps          
             1 E-point                                  100002Mbps          
             1 T-Mobile                                 100003Mbps          
             1 Net  Friends                             100004Mbps          
             2 Cogent Communications Poland Sp. z.o.o.  200001Mbps          
             2 E-point                                  200002Mbps          
             2 T-Mobile                                 200003Mbps          
             2 Net  Friends                             200004Mbps          
    
    8 rows selected.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      相关资源
      最近更新 更多