代码之家  ›  专栏  ›  技术社区  ›  Roma Kap

忽略斜线的Oracle正则表达式

  •  0
  • Roma Kap  · 技术社区  · 8 年前

    我试图创建regex,它忽略了斜线符号的选择。 例如,在我的专栏“Things”中有这样的内容:

    arbit
    t/obt 
    t/comp 
    t/dor
    cramp
    pod
    

    我只能输入:pod,tdor,arbit,tcomp

    我试过“ REGEXP_SUBSTR “-表达式,但可能不适合此问题。

    2 回复  |  直到 8 年前
        1
  •  2
  •   Allan    8 年前

    假设您有下表:

    $ select * from PERSONS;
    
    PersonID LastName FirstName ADDRESS CITY
    1          arbit     null      null  null           
    2          t/obt     null      null  null           
    3          t/comp    null      null  null           
    4          t/dor     null      null  null           
    5          cramp     null      null  null           
    6          pod       null      null  null
    

    你可以用下面的简单方法 replace 不使用regex的:

    select Replace(Lastname,'/','') from persons;
    -- you can omit the replacement part select Replace(Lastname,'/') from persons;
    
    --Replace(Lastname,'/','')--    
    arbit
    tobt
    tcomp
    tdor
    cramp
    pod
    

    如果确实需要使用regex进行复杂的搜索和替换: (这在您的情况下是不必要的)

    $ select REGEXP_REPLACE(Lastname, '/', '') from persons;
    
    --REGEXP_REPLACE(Lastname, '/', '')--
    arbit
    tobt
    tcomp
    tdor
    cramp
    pod
    

    也可以忽略替换零件,因为它是空的:

    select REGEXP_REPLACE(Lastname, '/') from persons
    
        2
  •  2
  •   Lukasz Szozda    8 年前

    您可以使用简单的替换来删除 / :

    SELECT things, REPLACE(things, '/', '')
    FROM tab