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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199139, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/SQL/functions/regular_expressions/index(115): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78