Search notes:

Oracle functions for regular expressions

regexp_substr extracts a portion (a substring) from a text that matches a given regular expression.
regexp_replace replaces a portion of a text that matches a regular expression with another text.
regexp_like is similar to the sql like operator, but searches for regular expressions rather than substrings.
regexp_instr returns the position at which a regular expression matches.
regexp_count returns how many times a given regular expression is matched in a string.

Example: word_boundaries

create table tq84_word_boundaries (
  txt  varchar2(50)
);

insert into tq84_word_boundaries values ('AFooABarAndABaz'       );
insert into tq84_word_boundaries values ('A FooA BarAndABaz'     );
insert into tq84_word_boundaries values ('A Foo, a Bar and a Baz');
insert into tq84_word_boundaries values ('A Foo without a Baz'   );
insert into tq84_word_boundaries values ('Foo Bar, Baz'          );
insert into tq84_word_boundaries values ('Is it a Foo?'          );
insert into tq84_word_boundaries values ('Bar-Foo-Baz'           );

select * from tq84_word_boundaries
  where
    regexp_like(txt, '\bFoo\b');

-- No records returned, Oracle does not support \b (at least not on 11i)


select * from tq84_word_boundaries
  where
    regexp_like(txt, '\sFoo\s');

-- TXT
-- --------------------------------------------------
-- A Foo without a Baz


select * from tq84_word_boundaries
  where
    regexp_like(txt, '(^|\s)Foo($|\s)');


-- TXT
-- --------------------------------------------------
-- A Foo without a Baz
-- Foo Bar, Baz



select * from tq84_word_boundaries
  where
    regexp_like(txt, '(^|\s|\W)Foo($|\s|\W)');

-- TXT
-- --------------------------------------------------
-- A Foo, a Bar and a Baz
-- A Foo without a Baz
-- Foo Bar, Baz
-- Is it a Foo?
-- Bar-Foo-Baz



drop table tq84_word_boundaries purge;
Github repository Oracle-Patterns, path: /SQL/functions/regular_expressions/word_boundaries.sql

See also

SQL Server: Regular expression functions
regular expressions
Oracle SQL functions

Index