1

I'm working with an Oracle DB and I'm trying to find and extract ALL occurrences in a string matching a specific pattern...

It's supposed to be 3 letters, 3 numbers and then maybe a letter or not

I tried this:

SELECT REGEXP_SUBSTR(my_column, '[A-Za-z]{3}(\d)(\d)(\d)') AS values 
FROM my_table

but it only returns the first occurrence.

Using

REGEXP_SUBSTR(my_column, '[A-Za-z]{3}(\d)(\d)(\d)', 0, 0, 'i')

doesn't work either

Does anybody have any ideas?

Edit:

I'm trying to extract it from PLSQL files. So its pretty much like SQL queries like

select * 
from abc123 
where some_value = 'some_value'
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
javaBeginner
  • 83
  • 2
  • 8

2 Answers2

4

Try this query to break ABC123CDE456FGHI789 squence

with mine as (select 'ABC123CDE456FGH789' hello from dual) 
select regexp_substr(hello, '[A-Za-z]{3}(\d){3}', 1, level) STR from mine
connect by regexp_substr(hello, '[A-Za-z]{3}(\d){3}', 1, level) is not null

Output

ABC123
CDE456
GHI789

For get specific postion then you want to use

select regexp_substr('ABC123CDE456FGH789', '[A-Za-z]{3}(\d){3}', 1, i) STR from dual

change i value as per position like

select regexp_substr('ABC123CDE456FGH789', '[A-Za-z]{3}(\d){3}', 1, 1) STR from dual

Output :

ABC123
Sanjay Radadiya
  • 1,254
  • 15
  • 22
1

Try to get number in 1.2.3 (suppose it is a domain of a country)

SELECT str,level,REGEXP_SUBSTR(str, '[0-9]', 1, LEVEL) AS substr
FROM (
SELECT country_domain str from country where regexp_like(country_domain, '[0-9]')
)
CONNECT BY LEVEL <= REGEXP_count(str, '[0-9]');

Output

STR    LEVEL SUBSTR
1.2.3   1     1
1.2.3   2     2
1.2.3   3     3
thanh ngo
  • 834
  • 5
  • 9