Oracle PL/SQL Tutorial/Regular Expressions Functions/REGEXP INSTR

Материал из SQL эксперт
Перейти к: навигация, поиск

Find the "s" and ignore case.

   <source lang="sql">

SQL> SQL> SELECT REGEXP_INSTR("This is a test for print SS","s",1,1,0,"i") position

 2  FROM dual;
 POSITION

        4

SQL></source>


Occurrence refers to the first, second, third, etc., occurrence of the pattern in S. The default is 1 (first).

Searching for the second "a" starting at position 1.



   <source lang="sql">

SQL> SELECT REGEXP_INSTR("This is a test for print a","a",1,2) position

 2  FROM dual;
 POSITION

       26

SQL></source>


Parameters is a field that may be used to define how one wants the search to proceed:

  1. i - to ignore case
  2. c - to match case
  3. n - to make the metacharacter dot symbol match new lines as well as other characters (more on this later in the chapter)
  4. m - to make the metacharacters ^ and $ match beginning and end of a line in a multiline string (more, later)

The default is "i".

The following example finds the "s" and match case.



   <source lang="sql">

SQL> SQL> SELECT REGEXP_INSTR("This is a test for printing SS","s",1,1,0,"c") position

 2  FROM dual;
 POSITION

        4

SQL></source>


Regexp_Instr

REGEXP_INSTR function returns a number for the position of matched pattern.

Unlike INSTR, REGEXP_INSTR cannot work from the end of the string backward.

The arguments for REGEXP_INSTR are:



   <source lang="sql">

REGEXP_INSTR(String to search, Pattern, [Position,[Occurrence, [Return-option, [Parameters]]]])</source>


== regexp_instr(comments, "[^ ]+", 1,/td>




   <source lang="sql">

SQL> SQL> create table history

 2  ( empno      NUMBER(4)
 3  , beginyear  NUMBER(4)
 4  , begindate  DATE
 5  , enddate    DATE
 6  , deptno     NUMBER(2)
 7  , msal       NUMBER(6,2)
 8  , comments   VARCHAR2(60)
 9  ) ;

Table created. SQL> SQL> SQL> insert into history values (9,2000,date "2000-01-01",date "2002-01-02",40, 950,"history for 9"); 1 row created. SQL> insert into history values (8,2000,date "2000-01-02", NULL ,20, 800,""); 1 row created. SQL> insert into history values (7,1988,date "2000-01-06",date "2002-01-07",30,1000,""); 1 row created. SQL> insert into history values (6,1989,date "2000-01-07",date "2002-01-12",30,1300,""); 1 row created. SQL> insert into history values (5,1993,date "2000-01-12",date "2002-01-10",30,1500,"history for 5"); 1 row created. SQL> insert into history values (4,1995,date "2000-01-10",date "2002-01-11",30,1700,""); 1 row created. SQL> insert into history values (3,1999,date "2000-01-11", NULL ,30,1600,""); 1 row created. SQL> insert into history values (2,1986,date "2000-01-10",date "2002-01-08",20,1000,"history for 2"); 1 row created. SQL> insert into history values (1,1987,date "2000-01-08",date "2002-01-01",30,1000,"history for 1"); 1 row created. SQL> insert into history values (7,1989,date "2000-01-01",date "2002-05-12",30,1150,"history for 7"); 1 row created. SQL> SQL> select comments

 2  from   history
 3  where  regexp_instr(comments, "[^ ]+", 1, 9) > 0;

no rows selected SQL> SQL> SQL> drop table history; Table dropped. SQL> SQL></source>


Regexp_Instr returns the location (beginning) of a pattern in a given string

REGEXP_INSTR extends the regular INSTR string function by allowing searches of regular expressions.

The simplest form of this function is:



   <source lang="sql">

REGEXP_INSTR(source_string, pattern_to_find)</source>


REGEXP_INSTR(x, pattern [, start [, occurrence [, return_option [, match_option]]]]) searches for pattern in x.

REGEXP_INSTR() returns the position at which pattern occurs.

The position starts at number 1.

The following example returns the position that matches the regular expression lalpha: {4} using REGEXP_INSTR():



   <source lang="sql">

SQL> SELECT REGEXP_INSTR("abcedfghijklumnoprstuvwxyzabcedfghijklumnoprstuvwxyzabcedfghijklumnoprstuvwxyz", "lalpha:{4}") AS result

 2  FROM dual;
   RESULT

       12

SQL></source>


Return-option returns the position of the start or end of the matched string.

The default is 0, which returns the starting position of the pattern in the target.

A value of 1 returns the starting position of the next character following the pattern match.



   <source lang="sql">

SQL> SQL> SELECT REGEXP_INSTR("This is a test","a",1,2,0) position

 2  FROM dual;
 POSITION

        0

SQL></source>


Returns the position of the second occurrence that matches the letter o starting at position 10 using REGEXP_INSTR()

   <source lang="sql">

SQL> SQL> SELECT REGEXP_INSTR("abcedfghijklumnoprstuvwxyzabcedfghijklumnoprstuvwxyzabcedfghijklumnoprstuvwxyz", "o", 10, 2) AS result

 2  FROM dual;
   RESULT

       42

SQL></source>


Returns the position of the second occurrence that matches the regular expression salpha:{3} starting at position 1:

   <source lang="sql">

SQL> SQL> SELECT REGEXP_INSTR("But, soft! What light through yonder window softly breaks?", "salpha:{3}", 1, 2) AS result

 2  FROM dual;
   RESULT

       45

SQL></source>


SELECT REGEXP_INSTR("Two is bigger than One","One") where_it_is

   <source lang="sql">

2 FROM dual; WHERE_IT_IS


        20

SQL></source>


Specify any series of letters and find matches, just like INSTR

   <source lang="sql">

SQL> SQL> -- create demo table SQL> create table myTable(

 2    id           NUMBER(2),
 3    value        VARCHAR(50)
 4  );

Table created. SQL> SQL> insert into myTable(id, value)values(1,"1234 4th St. Vancouver"); 1 row created. SQL> insert into myTable(id, value)values(2,"4 Maple Ct. New York"); 1 row created. SQL> insert into myTable(id, value)values(3,"4321 Green Blvd. London"); 1 row created. SQL> insert into myTable(id, value)values(4,"33 Third St. Toronto"); 1 row created. SQL> insert into myTable(id, value)values(5,"One First Drive. Queen"); 1 row created. SQL> insert into myTable(id, value)values(6,"1664 1/2 Springhill Ave"); 1 row created. SQL> insert into myTable(id, value)values(7,"665 Fall Ave. Linken"); 1 row created. SQL> SQL> select * from mytable;

       ID VALUE

--------------------------------------------------
        1 1234 4th St. Vancouver
        2 4 Maple Ct. New York
        3 4321 Green Blvd. London
        4 33 Third St. Toronto
        5 One First Drive. Queen
        6 1664 1/2 Springhill Ave
        7 665 Fall Ave. Linken

7 rows selected. SQL> SQL> SQL> SELECT value, REGEXP_INSTR(value,"ing") where_it_is

 2  FROM myTable
 3  WHERE REGEXP_INSTR(value,"ing") > 0;

VALUE WHERE_IT_IS


-----------

1664 1/2 Springhill Ave 13 SQL> SQL> SQL> drop table myTable; Table dropped. SQL> SQL></source>


The Return-option is set to 1 to indicate the end of the found pattern

   <source lang="sql">

SQL> SQL> SELECT REGEXP_INSTR("This is a test","a",1,2,1) position

 2  FROM dual;
 POSITION

        0

SQL></source>


The simplest regular expression matches letters, letter for letter

   <source lang="sql">

SQL> SQL> -- create demo table SQL> create table myTable(

 2    id           NUMBER(2),
 3    value        VARCHAR(50)
 4  );

Table created. SQL> SQL> insert into myTable(id, value)values(1,"1234 4th St. Vancouver"); 1 row created. SQL> insert into myTable(id, value)values(2,"4 Maple Ct. New York"); 1 row created. SQL> insert into myTable(id, value)values(3,"4321 Green Blvd. London"); 1 row created. SQL> insert into myTable(id, value)values(4,"33 Third St. Toronto"); 1 row created. SQL> insert into myTable(id, value)values(5,"One First Drive. Queen"); 1 row created. SQL> insert into myTable(id, value)values(6,"1664 1/2 AAA Ave"); 1 row created. SQL> insert into myTable(id, value)values(7,"665 Fall Ave. Linken"); 1 row created. SQL> SQL> select * from mytable;

       ID VALUE

--------------------------------------------------
        1 1234 4th St. Vancouver
        2 4 Maple Ct. New York
        3 4321 Green Blvd. London
        4 33 Third St. Toronto
        5 One First Drive. Queen
        6 1664 1/2 AAA Ave
        7 665 Fall Ave. Linken

7 rows selected. SQL> SQL> SELECT value, REGEXP_INSTR(value,"Ave") where_it_is

 2  FROM myTable
 3  WHERE REGEXP_INSTR(value,"Ave") > 0;

VALUE WHERE_IT_IS


-----------

1664 1/2 AAA Ave 14 665 Fall Ave. Linken 10 SQL> SQL> drop table myTable; Table dropped. SQL> SQL></source>