`

oracle 10g支持正则表达式

阅读更多

近期调整了表结构,令 site_id 为 number(9),测试过程中发现有些包含有

i_site_id INTEGER(2);

这样的声明,导致 site_id 大于两位时报错

用 plsql developer 的 find data objects 的正则表达式功能无法忽略大小写,因此直接从源中查

SELECT t.name, t.line, t.text
  FROM user_source t
 WHERE
-- 全部类型的源码
-- TYPE IN ("PROCEDURE","PACKAGE","PACKAGE BODY","TRIGGER","FUNCTION","TYPE") AND 
 regexp_like(t.text, "(number|integer)\([1-8]\)", "i")
 ORDER BY t.name, t.line;

解释:(number|integer)\([1-8]\) 匹配:number(1~8) 这样的源码,"i" 代表忽略大小写

 

ORACLE终于在10G中提供了对正则表达式的支持,以前那些需要通过LIKE来进行的复杂的匹配就可以通过使用正则表达式更简单的实现。

ORACLE中的支持正则表达式的函数主要有下面四个:


1
REGEXP_LIKE :与LIKE的功能相似

2REGEXP_INSTR :与INSTR的功能相似

3REGEXP_SUBSTR :与SUBSTR的功能相似

4REGEXP_REPLACE :与REPLACE的功能相似

在新的函数中使用正则表达式来代替通配符‘%’和‘_’。

正则表达式由标准的元字符(metacharacters)所构成:

'^' 匹配输入字符串的开始位置,在方括号表达式中使用,此时它表示不接受该字符集合。

'$' 匹配输入字符串的结尾位置。如果设置了 RegExp 对象的 Multiline 属性,则 $ 也匹配 'n' 'r'

'.' 匹配除换行符 n之外的任何单字符。

'?' 匹配前面的子表达式零次或一次。

'+' 匹配前面的子表达式一次或多次。

'*' 匹配前面的子表达式零次或多次。

'|' 指明两项之间的一个选择。例子'^([a-z]+|[0-9]+)$'表示所有小写字母或数字组合成的字符串。

'( )' 标记一个子表达式的开始和结束位置。

'[]' 标记一个中括号表达式。

'{m,n}' 一个精确地出现次数范围,m=<出现次数<=n'{m}'表示出现m次,'{m,}'表示至少出现m次。

num 匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。

字符簇:

[[:alpha:]] 任何字母。

[[:digit:]] 任何数字。

[[:alnum:]] 任何字母和数字。

[[:space:]] 任何白字符。

[[:upper:]] 任何大写字母。

[[:lower:]] 任何小写字母。

[[:punct:]] 任何标点符号。

[[:xdigit:]] 任何16进制的数字,相当于[0-9a-fA-F]

各种操作符的运算优先级

转义符

(), (?:), (?=), [] 圆括号和方括号

*, +, ?, {n}, {n,}, {n,m} 限定符

^, $, anymetacharacter 位置和顺序

| “操作

下面通过几个例子来具体说明这几个新函数的使用方法:

SQL> create table sunwg (id varchar2(100));

Table created.

SQL> insert into sunwg values ('<a href="http://sunwgneuqsoft.itpub.net/post/34741/447698">常见SQL访问索引的方式</a>');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from sunwg;

ID

----------------------------------------------------------------------------------------------------

<a href="http://sunwgneuqsoft.itpub.net/post/34741/447698">常见SQL访问索引的方式</a>

1, REGEXP_LIKE

REGEXP_LIKELIKE类似,用REGEXP_LIKE能实现的操作大部分都可以用LIKE实现,不过要简单方便得多。

<a>目标:查询表sunwg中是否存在类似与3XX41的记录?

LIKE

select * from sunwg where id like '%3__41%';

REGEXP_LIKE

select * from sunwg where regexp_like(id,'3..41');

<b>目标:查询表sunwg中是否存在类似与3XX41的记录,并且XX必须是数字?

LIKE

这个LIKE我就想出来很好的实现办法了,唯一想到就是截取出来后判断该字符串是不是纯数字的。

REGEXP_LIKE

select * from sunwg where regexp_like(id,'3[0-9]{2}41');

REGEXP_LIKE则可以简单快捷的得到结果。其他几个函数也都有类似的情况,下面的函数就不具体比较差异了,仅仅给出常用的用法。

2, REGEXP_INSTR

<a>目标:查询表sunwg中是否存在类似与3XX41的字符串第一次出现的位置?

SQL> select regexp_instr(id,'3..41',1,1) from sunwg;

REGEXP_INSTR(ID,'3..41',1,1)

----------------------------

46

SQL> select substr(id,46,5) from sunwg;

SUBST

-----

34741

3, REGEXP_SUBSTR

<a>目标:截取出表sunwg中的URL地址?

SQL> select regexp_substr(id,'http[0-9a-zA-Z/:.]+') from sunwg;

REGEXP_SUBSTR(ID,'HTTP[0-9A-ZA-Z/:.]+')

----------------------------------------------------------------------------------------------------

http://sunwgneuqsoft.itpub.net/post/34741/447698

4, REGEXP_REPLACE

<a>目标:替换表sunwg中的URL的地址为www.163.com?

SQL> select regexp_replace(id,'http[0-9a-zA-Z/:.]+','www.163.com') from sunwg;

REGEXP_REPLACE(ID,'HTTP[0-9A-ZA-Z/:.]+','WWW.163.COM')

------------------------------------------------------------------------------------------------------------------------------------------------------

<a href="www.163.com">常见SQL访问索引的方式</a>

从上面的例子可以看得出来这几个支持正则表达式的函数是十分强大的,合理的加以使用一定会使你写出的SQL更加简单高效。 

regexp_substr

regexp_substr (string, pattern, position) regexp_substr (string, pattern, position, occurence) regexp_substr (string, pattern, position, occurence, parameters)

parameters can be a combination of

regexp_substr (string, pattern)
  • i: to match case insensitively
  • c: to match case sensitively
  • n: to make the dot (.) match new lines as well
  • m: to make ^ and $ match beginning and end of a line in a multiline string
regexp_substr is an Oracle SQL function that enables regular expressions in queries. It enhances the 'traditional' substr.

Links

See also On splitting a string into words with regular expressions where a function uses regexp_substr to split a string.
Then there is also safe_to_number() where regexp_substr is used to convert strings to numbers.

regexp_instr

regexp_instr
(string, pattern) regexp_instr (string, pattern, position) regexp_instr (string, pattern, position, occurence) regexp_instr (string, pattern, position, occurence, return-option) regexp_instr (string, pattern, position, occurence, return-option, parameters)

Parameters

parameters can be a combination of
  • i: to match case insensitively
  • c: to match case sensitively
  • n: to make the dot (.) match new lines as well
  • m: to make ^ and $ match beginning and end of a line in a multiline string
  • x: to ignore white spaces.
regexp_instr is an Oracle SQL function that enables regular expressions in queries. It enhances the 'traditional' instr.

regexp_like

regexp_like
(string, pattern); regexp_like (string, pattern, parameters);
parameters can be a combination of
  • i: to match case insensitively
  • c: to match case sensitively
  • n: to make the dot (.) match new lines as well
  • m: to make ^ and $ match beginning and end of a line in a multiline string
regexp_like is an Oracle SQL function that enables regular expressions in queries. It enhances the «traditional» like.
regexp_like is a pattern condition.

Demonstration

create table strings (
  str  varchar2(30)
);

create table patterns (
  pat  varchar2(50),
  dsc  varchar2(30)
);
insert into patterns values ('^[[:digit:]]{3}-[[:digit:]]{2}-[[:digit:]]{4}
insert into strings values ('987-65-4321');
insert into strings values ('hello foo bar');
insert into strings values ('4987-65-4321');
insert into strings values ('hello FOO BAR');
insert into strings values ('-4.55');
insert into strings values ('987-65-43213');
insert into strings values ('4.55');
insert into strings values ('hello bar bar');
insert into strings values (' 4.55');
insert into strings values ('1234567890');
insert into strings values ('hello FOO FOO');
select
  str,dsc 
from 
  strings cross join patterns
where
  regexp_like(str, pat) ;
STR                            DSC
------------------------------ ------------------------------
987-65-4321                    Social security number
hello bar bar                  Repeated words
hello FOO FOO                  Repeated words
hello foo bar                  Only lowercase words
hello bar bar                  Only lowercase words
1234567890                     Only digits
987-65-4321                    At least one digit
4987-65-4321                   At least one digit
-4.55                          At least one digit
987-65-43213                   At least one digit
4.55                           At least one digit
 4.55                          At least one digit
1234567890                     At least one digit
-4.55                          Number
4.55                           Number
1234567890                     Number

regexp_replace

regexp_replace
(string, pattern) regexp_replace (string, pattern, replace-string) regexp_replace (string, pattern, replace-string, position) regexp_replace (string, pattern, replace-string, position, occurence) regexp_replace (string, pattern, replace-string, position, occurence, parameters)
parameters can be a combination of
  • i: to match case insensitively
  • c: to match case sensitively
  • n: to make the dot (.) match new lines as well
  • m: to make ^ and $ match beginning and end of a line in a multiline string
regexp_substr is an Oracle SQL function that enables regular expressions in queries. It enhances the 'traditional' substr.
regexp_replace is an Oracle SQL function that enables regular expressions in queries. It enhances the 'traditional' replace.

Demonstration

create table strings (
  str varchar2(30)
);

create table patterns (
  pat  varchar2(60),
  repl varchar2(30),
  dsc  varchar2(30)
);
insert into patterns values ('^[[:space:]]*[^[:space:]]+[[:space:]]+([^[:space:]]+).*', '\1', 'The 2nd word');
insert into patterns values ('^[^[:digit:]]*([[:digit:]]*\.?[[:digit:]]+).*'          , '\1', 'The 1st number');
insert into patterns values ('^[^[:upper:]]*([[:upper:]]+).*'                         , '\1', 'Uppercase word');
insert into strings values ('foo  bar   baz');
insert into strings values ('bla   MOO   82.22    7.34  bla');
insert into strings values ('  one   two 3 four  ');
column found format a20

select
  str,
  regexp_replace(str, pat, repl) found,
  dsc
from
  strings cross join patterns
where
  regexp_instr(str,pat) > 0;
STR                            FOUND                DSC
------------------------------ -------------------- --------------------
foo  bar   baz                 bar                  The 2nd word
bla   MOO   82.22    7.34  bla MOO                  The 2nd word
  one   two 3 four             two                  The 2nd word
bla   MOO   82.22    7.34  bla 82.22                The 1st number
  one   two 3 four             3                    The 1st number
bla   MOO   82.22    7.34  bla MOO                  Uppercase word

Links

, 'Social security number'); insert into patterns values ('[^[:alpha:]]([[:alpha:]]+)[^[:alpha:]] *\1' , 'Repeated words'); insert into patterns values ('^([[:lower:]]| )*

___FCKpd___5
___FCKpd___6
___FCKpd___7
___FCKpd___8
parameters can be a combination of
  • i: to match case insensitively
  • c: to match case sensitively
  • n: to make the dot (.) match new lines as well
  • m: to make ^ and $ match beginning and end of a line in a multiline string
regexp_substr is an Oracle SQL function that enables regular expressions in queries. It enhances the 'traditional' substr.
regexp_replace is an Oracle SQL function that enables regular expressions in queries. It enhances the 'traditional' replace.

Demonstration

___FCKpd___9
___FCKpd___10
___FCKpd___11
___FCKpd___12
___FCKpd___13

Links

分享到:
评论

相关推荐

    Oracle 10g 针对正则表达式的支持分析

    ### Oracle 10g 对正则表达式的支持分析 #### 一、引言 随着信息技术的发展,数据库管理系统(DBMS)的功能也在不断扩展和完善。Oracle Database 作为业界领先的数据库产品之一,在其10g版本中引入了对正则表达式...

    Oracle数据库正则表达式

    Oracle 提供了四个支持正则表达式的函数: 1. REGEXP_LIKE:比较一个字符串是否与正则表达式匹配 2. REGEXP_INSTR:在字符串中查找正则表达式,并且返回匹配的位置 3. REGEXP_SUBSTR:返回与正则表达式匹配的子字符...

    Oracle正则表达式详解(用法+实例)

    Oracle 10g 引入了一系列新的函数来支持正则表达式的使用,包括 `REGEXP_LIKE`、`REGEXP_INSTR`、`REGEXP_SUBSTR` 和 `REGEXP_REPLACE`。这些函数的使用方式类似于传统的 SQL 函数 `LIKE`、`INSTR`、`SUBSTR` 和 `...

    Oracle 10g正则表达式

    Oracle 10g引入了对正则表达式的支持,使得在数据库查询中处理复杂匹配变得更加简单。在10g版本之前,我们通常依赖`LIKE`和通配符`%`和`_`来完成此类任务,但正则表达式提供了更强大的功能和灵活性。 在Oracle 10g...

    oracle_10g正则表达式_REGEXP_LIKE_用法

    Oracle 10g支持多种正则表达式相关的函数,包括但不限于: 1. **`REGEXP_LIKE`**:用于判断一个字符串是否符合特定的正则表达式模式。 2. **`REGEXP_INSTR`**:用于查找一个字符串中满足正则表达式模式的第一个位置...

    ORACLESQL正则表达式.pdf

    正则表达式是在 Oracle 10g 中添加的新特性,用于匹配字符串中的模式。下面是 ORACLE SQL 正则表达式的相关知识点: 匹配字符 * `[:alnum:]` 匹配任何字母和数字 * `[:alpha:]` 匹配任何字母 * `[:blank:]` 匹配...

    Oracle Database 10g 中的正则表达式

    Oracle Database 10g 引入了正则表达式特性,极大地增强了处理和查询文本数据的能力。正则表达式是一种模式匹配的语言,用于描述复杂的文本结构,广泛应用于各种编程语言和UNIX工具。在Oracle 10g中,正则表达式通过...

    Oracle 总结的正则表达式带有详细的例子与解释

    在Oracle 10g及更高版本中,Oracle引入了对IEEE POSIX标准的正则表达式支持,使得SQL语句能进行更复杂的模式匹配。 正则表达式的核心在于元字符,它们具有特殊的含义: 1. `^`:表示匹配一行的开始。 2. `$`:表示...

    Oracle 正则表达式介绍

    - REGEXP_INSTR:返回子串在目标字符串中首次出现的位置,支持正则表达式模式。例如,`REGEXP_INSTR(string, 'pattern')`返回pattern在string中出现的第一个位置。 - REGEXP_SUBSTR:返回满足正则表达式模式的子串...

    oracle正则表达式.ppt

    Oracle正则表达式是Oracle 10g及后续版本引入的一种强大的文本处理工具,它极大地增强了SQL查询的灵活性。...在10g之后的版本中,Oracle继续优化和扩展了正则表达式支持,使其成为处理和分析文本数据的强大工具。

    Oracle正则表达式.pdf

    Oracle 支持正则表达式的内置函数: * `REGEXP_LIKE`:用于搜索字符串是否匹配正则表达式 * `REGEXP_REPLACE`:用于使用正则表达式替换字符串 * `REGEXP_SUBSTR`:用于使用正则表达式提取子字符串 REGEXP_LIKE ...

    Oracle正则表达式的应用

    - 该函数类似于标准的`like`操作,但支持正则表达式模式匹配。例如: ```sql select * from test_table where regexp_like(field_1, '^1234') ``` 这个查询将返回所有`field_1`字段以"1234"开头的记录。 2. `...

    Oracle正则表达式

    在Oracle 10g版本开始,数据库引入了对正则表达式的支持。正则表达式是一种强大的工具,用于模式匹配和文本处理,可以用来搜索、替换、提取等操作。通过正则表达式,用户可以在SQL语句中实现对字符串数据的高级操作...

    JDK+JQUERY+正则表达式API

    JavaScript的正则表达式则是直接嵌入在字符串中的,如`/\\d+/g`,它同样能实现类似的功能。 这三个主题的结合,构成了一个全面的开发工具箱,无论是后端的Java服务开发,还是前端的网页交互,甚至是数据的验证和...

    jakarta-oro-2.0.8 java正则表达式扩展

    Jakarta ORO,全称是Oracle Regular Expressions for Java,是由Apache软件基金会开发的一个Java正则表达式库。它是对Java内置的正则表达式功能的扩展,提供了更丰富的功能和更高的性能。Jakarta ORO在Java社区中...

    orcl_reg.zip_oracle10G

    在实际应用中,Oracle 10g的正则表达式支持预编译模式,通过`REGEXP_PATTERN`函数创建一个预编译的正则表达式对象,这能提高执行效率。此外,Oracle还提供了诸如`REGEXP_INSTR`(返回匹配的位置)、`REGEXP_COUNT`...

Global site tag (gtag.js) - Google Analytics