Oracle PL/SQL Tutorial/Regular Expressions Functions/REGEXP LIKE — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:08, 26 мая 2010
Содержание
- 1 Asking for the presence of a "g" or a "p"
- 2 Asking for the presence of a "g" or a "p" with REGEXP_LIKE
- 3 Regexp_Like
- 4 regexp_like(comments, "([^ ]+ ){8,}")
- 5 regexp_like(comments, " f[a-z]* ","i")
- 6 REGEXP_LIKE(x, pattern [, match_option]) searchs x for the regular expression pattern.
- 7 Retrieves employees whose first name starts with J or j.
- 8 Using regexp_like(comments, "([[:alnum:]+[:punct:]]+space:+){8,}")
Asking for the presence of a "g" or a "p"
The "%" sign metacharacter matches zero, one, or more characters.
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
2 FROM myTable
3 WHERE value LIKE("%g%")OR value LIKE ("%p%");
VALUE
--------------------------------------------------
4 Maple Ct. New York
1664 1/2 Springhill Ave
SQL>
SQL>
SQL> drop table myTable;
Table dropped.
SQL>
SQL>
Asking for the presence of a "g" or a "p" with REGEXP_LIKE
The order of occurrence of [gp] or [pg] is irrelevant.
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
2 FROM myTable
3 WHERE REGEXP_LIKE(value,"[gp]");
VALUE
--------------------------------------------------
4 Maple Ct. New York
1664 1/2 Springhill Ave
SQL>
SQL>
SQL> drop table myTable;
Table dropped.
SQL>
SQL>
Regexp_Like
The complete function definition is:
REGEXP_LIKE(String to search, Pattern, [Parameters]),
where
String to search, Pattern, and Parameters are the same as for REGEXP_INSTR.
As with REGEXP_INSTR, the Parameters argument is usually used only in special situations.
18. 3. REGEXP_LIKE 18. 3. 1. <A href="/Tutorial/Oracle/0360__Regular-Expressions-Functions/REGEXPLIKExpatternmatchoptionsearchsxfortheregularexpressionpattern.htm">REGEXP_LIKE(x, pattern [, match_option]) searchs x for the regular expression pattern.</a> 18. 3. 2. Regexp_Like 18. 3. 3. <A href="/Tutorial/Oracle/0360__Regular-Expressions-Functions/Askingforthepresenceofagorap.htm">Asking for the presence of a "g" or a "p"</a> 18. 3. 4. <A href="/Tutorial/Oracle/0360__Regular-Expressions-Functions/AskingforthepresenceofagorapwithREGEXPLIKE.htm">Asking for the presence of a "g" or a "p" with REGEXP_LIKE</a> 18. 3. 5. <A href="/Tutorial/Oracle/0360__Regular-Expressions-Functions/RetrievesemployeeswhosefirstnamestartswithJorj.htm">Retrieves employees whose first name starts with J or j.</a> 18. 3. 6. <A href="/Tutorial/Oracle/0360__Regular-Expressions-Functions/regexplikecommentsfazi.htm">regexp_like(comments, " f[a-z]* ","i")</a> 18. 3. 7. <A href="/Tutorial/Oracle/0360__Regular-Expressions-Functions/regexplikecomments8.htm">regexp_like(comments, "([^ ]+ ){8,}")</a> 18. 3. 8. <A href="/Tutorial/Oracle/0360__Regular-Expressions-Functions/Usingregexplikecommentsalnumpunctspace8.htm">Using regexp_like(comments, "([[:alnum:]+[:punct:]]+space:+){8,}")</a>
regexp_like(comments, "([^ ]+ ){8,}")
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>
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>
SQL>
SQL>
SQL> select comments
2 from history
3 where regexp_like(comments, "([^ ]+ ){8,}");
no rows selected
SQL>
SQL> drop table history;
Table dropped.
regexp_like(comments, " f[a-z]* ","i")
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>
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>
SQL> select regexp_replace(comments, " f[a-z]* "," ? ",1,1,"i")
2 from history
3 where regexp_like(comments, " f[a-z]* ","i");
REGEXP_REPLACE(COMMENTS,"F[A-Z]*","?",1,1,"I")
----------------------------------------------------------------------------------------------------
history ? 9
history ? 5
history ? 2
history ? 1
history ? 7
SQL>
SQL> drop table history;
Table dropped.
SQL>
REGEXP_LIKE(x, pattern [, match_option]) searchs x for the regular expression pattern.
You can provide an optional match_option string to indicate the default matching.
- "c", which specifies case sensitive matching (default).
- "i", which specifies case insensitive matching.
- "n", which allows you to use the match-any-character operator.
- "m", which treats x as multiple line.
The following example retrieves employees whose date of birth is between 1995 and 1998 using REGEXP_LIKE():
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL>
SQL>
SQL> SELECT id, first_name, last_name, start_date FROM employee
2 WHERE REGEXP_LIKE(TO_CHAR(start_date, "YYYY"), "^199[5-8]$");
ID FIRST_NAME LAST_NAME START_DAT
---- ---------- ---------- ---------
01 Jason Martin 25-JUL-96
08 James Cat 17-SEP-96
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
Retrieves employees whose first name starts with J or j.
SQL>
SQL>
SQL>
SQL> -- create demo table
SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values ("01","Jason", "Martin", to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto", "Programmer")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("02","Alison", "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("03","James", "Smith", to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("04","Celia", "Rice", to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("05","Robert", "Black", to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("06","Linda", "Green", to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York", "Tester")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("07","David", "Larry", to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York", "Manager")
3 /
1 row created.
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)
2 values("08","James", "Cat", to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
3 /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /
ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer
02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester
03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester
04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager
05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester
06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester
07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager
08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester
8 rows selected.
SQL>
SQL> SELECT id, first_name, last_name, start_date FROM employee WHERE REGEXP_LIKE(first_name, "^j", "i");
ID FIRST_NAME LAST_NAME START_DAT
---- ---------- ---------- ---------
01 Jason Martin 25-JUL-96
03 James Smith 12-DEC-78
08 James Cat 17-SEP-96
SQL> /
ID FIRST_NAME LAST_NAME START_DAT
---- ---------- ---------- ---------
01 Jason Martin 25-JUL-96
03 James Smith 12-DEC-78
08 James Cat 17-SEP-96
SQL>
SQL>
SQL>
SQL> -- clean the table
SQL> drop table Employee
2 /
Table dropped.
SQL>
Using regexp_like(comments, "([[:alnum:]+[:punct:]]+space:+){8,}")
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>
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>
SQL>
SQL> select comments
2 from history
3 where regexp_like(comments, "([[:alnum:]+[:punct:]]+[[:space:]]+){8,}");
no rows selected
SQL>
SQL> drop table history;
Table dropped.