如何过滤包含字符和数字的某些组合的字符串

时间:2019-04-04 09:06:41

标签: regex oracle

我想问我如何过滤可能包含2种模式的列“产品”:

  1. 以3个字符“ A-Z”或“ a-z”开头,后跟数字 或

例如:ABC1258,acb548796

  1. 以字符“ K”开头,后跟数字,然后是“ _”,然后是其他数字

例如:K123233_06565,K435_245 谢谢

2 个答案:

答案 0 :(得分:1)

这里是一个使用正则表达式的选项。

SQL> with test (col) as
  2    (select 'ABC1258'       from dual union all
  3     select 'K435_245'      from dual union all
  4     select 'AB_445'        from dual union all
  5     select 'KA234_23'      from dual union all
  6     select 'K12_BC'        from dual union all
  7     select 'XXX1243124YYY' from dual
  8    )
  9  select col
 10  from test
 11  where regexp_like(col, '^[[:alpha:]]{3}\d+$')
 12     or regexp_like(col, '^K\d+_\d+$');

COL
-------------
ABC1258
K435_245

SQL>

答案 1 :(得分:0)

这也是一种实现方法:

with tab as(
  select 'ABC1258' as str from dual union all
  select 'abc548796'as str from dual union all
  select 'K123233_06565'as str from dual union all
  select 'K435_245'as str from dual union all
  select 'X435_245'as str from dual union all
  select 'A435_245'as str from dual union all
  select 'KL435_245'as str from dual 
)

select *
from tab
where REGEXP_LIKE (str, '^([A-Za-z]{3}[0-9]+)|(K[0-9]+_).*$');

结果

ABC1258
abc548796
K123233_06565
K435_245