REGEXP_REPLACE 和REGEXP_SUBSTR函数的用法以及在其中如何使用正则表达式

正则表达式--- REGEXP_REPLACE 函数


转载请注明出处:http://www.jb51.cc/article/p-getsvitq-dk.html



这题又是考正则表达式,我们先根据题意,操作如下:

  1. hr@OCM>col"PHONENUMBER"fora50
  2. hr@OCM>SELECTphone_number,REGEXP_REPLACE(phone_number,'([[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4})','(\1)\2-\3')"PHONENUMBER"
  3. 2FROMemployees;
  4. PHONE_NUMBERPHONENUMBER
  5. ----------------------------------------------------------------------
  6. 650.507.9833(650)507-9833
  7. 650.507.9844(650)507-9844
  8. 515.123.4444(515)123-4444
  9. 011.44.1644.429264011.44.1644.429264
  10. 011.44.1644.429263011.44.1644.429263
  11. 011.44.1644.429262011.44.1644.429262
  12. 省略结果。。。。。
  13. 650.501.4876(650)501-4876
  14. 650.507.9811(650)507-9811
  15. 650.507.9822(650)507-9822
  16. 107rowsselected.

根据查询结果可以得出正确答案是:C



*************************************************************

REGEXP_REPLACE函数

让我们首先看一下传统的REPLACE SQL函数,它把一个字符串用另一个字符串来替换。假设您的数据在正文中有不必要的空格,您希望用单个空格来替换它们。利用REPLACE函数,您需要准确地列出您要替换多少个空格。然而,多余空格的数目在正文的各处可能不是相同的。下面的示例在JoeSmith之间有三个空格。REPLACE函数的参数指定要用一个空格来替换两个空格。在这种情况下,结果在原来的字符串的JoeSmith之间留下了一个额外的空格。

  1. SELECTREPLACE('JoeSmith','','')
  2. ASreplace
  3. FROMdual
  4. REPLACE
  5. ---------
  6. JoeSmith

REGEXP_REPLACE函数把替换功能向前推进了一步,其语法在表 9中列出。以下查询用单个空格替换了任意两个或更多的空格。( )子表达式包含了单个空格,它可以按{2,}的指示重复两次或更多次。

  1. SELECTREGEXP_REPLACE('JoeSmith',
  2. '(){2,}','')
  3. ASRX_REPLACE
  4. FROMdual
  5. RX_REPLACE
  6. ----------
  7. JoeSmith
注释:
REGEXP_REPLACE('Joe Smith','( ){2,' ')
表示在被搜索的字符串JoeSmith中那些符合(或叫匹配)正则表达式(){2,}的子字符串(这里即为三个空格符号)将被这里的REGEXP_REPLACE函数里的第三个参数值即单个空格符号所代替。

后向引用

正则表达式的一个有用的特性是能够存储子表达式供以后重用;这也被称为后向引用(在表 10 中对其进行了概述)。它允许复杂的替换功能,如在新的位置上交换模式或显示重复出现的单词或字母。子表达式的匹配部分保存在临时缓冲区中。缓冲区从左至右进行编号,并利用\digit 符号进行访问,其中digit 是 1 到 9 之间的一个数字,它匹配第digit 个子表达式,子表达式用一组圆括号来显示。

接下来的例子显示了通过按编号引用各个子表达式将姓名 Ellen Hildi Smith 转变为 Smith,Ellen Hildi。


  1. SELECTREGEXP_REPLACE(
  2. 'EllenHildiSmith',
  3. '(.*)(.*)(.*)','\3,\1\2')
  4. FROMdual
  5. REGEXP_REPLACE('EL
  6. ------------------
  7. Smith,EllenHildi

该 SQL 语句显示了用圆括号括住的三个单独的子表达式。每一个单独的子表达式包含一个匹配元字符 (.),并紧跟着 * 元字符,表示任何字符(除换行符之外)都必须匹配零次或更多次。空格将各个子表达式分开,空格也必须匹配。圆括号创建获取值的子表达式,并且可以用\digit 来引用。第一个子表达式被赋值为 \1 ,第二个 \2,以此类推。这些后向引用被用在这个函数的最后一个参数 (\3,\1 \2) 中,这个函数有效地返回了替换子字符串,并按期望的格式来排列它们(包括逗号和空格)。表 11 详细说明了该正则表达式的各个组成部分。

注释:
REGEXP_REPLACE('Ellen Hildi Smith','(.*) (.*) (.*)',\1 \2')
表示在被搜索的字符串Ellen Hildi Smith中那些符合(或叫匹配)正则表达式(.*) (.*) (.*)【其中该正则表达式中有空格将各个子表达式分开,空格也必须匹配】的子字符串(这里即为Ellen Hildi Smith)将被这里的REGEXP_REPLACE函数里的第三个参数值即\3,\1 \2所代替。\1代表的是正则表达式(.*) (.*) (.*)中的第一个以圆括号来表示的子表达式(.*),而该(.*)的值为Ellen\3和\2也以此类推。


后向引用对替换、格式化和代替值非常有用,并且您可以用它们来查找相邻出现的值。接下来的例子显示了使用REGEP_SUBSTR 函数来查找任意被空格隔开的重复出现的字母数字值。显示的结果给出了识别重复出现的单词is 的子字符串。


  1. SELECTREGEXP_SUBSTR(
  2. 'Thefinaltestisistheimplementation',
  3. ([[:alnum:]]+)([[:space:]]+)\1')ASsubstr
  4. FROMdual
  5. SUBSTR
  6. ------
  7. isis
注释:
正则表达式([[:alnum:]]+)([[:space:]]+)\1,等价于([[:alnum:]]+)([[:space:]]+)([[:alnum:]]+),即这里的\1表示的就是([[:alnum:]]+)([[:space:]]+)里的第一个以圆括号来表示的子表达式([[:alnum:]]+)。

REGEXP_SUBSTR(
'Thefinaltestisistheimplementation',‘([[:alnum:]]+)([[:space:]]+)\1')
表示的是显示在被搜索的字符串Thefinaltestisistheimplementation中那些符合(或叫匹配)正则表达式 ([[:alnum:]]+)([[:space:]]+)\1的子字符串(这里即为‘is is’)。

来自官方文档:




Purpose

REGEXP_REPLACE extends the functionality of the REPLACE function by letting you search a string for a regular expression pattern. By default,the function returnssource_char with every occurrence of the regular expression pattern replaced withreplace_string. The string returned is in the same character set assource_char. The function returnsVARCHAR2 if the first argument is not a LOB and returnsCLOB if the first argument is a LOB.

This function complies with the POSIX regular expression standard and the Unicode Regular Expression Guidelines. For more information,please refer toAppendix C,"Oracle Regular Expression Support".

  • source_char is a character expression that serves as the search value. It is commonly a character column and can be of any of the datatypesCHAR,VARCHAR2,NCHAR,NVARCHAR2,CLOB orNCLOB.

  • pattern is the regular expression. It is usually a text literal and can be of any of the datatypesCHAR,orNVARCHAR2. It can contain up to 512 bytes. If the datatype ofpattern is different from the datatype ofsource_char,Oracle Database convertspattern to the datatype ofsource_char. For a listing of the operators you can specify inpattern,"Oracle Regular Expression Support".

  • replace_string can be of any of the datatypes CHAR, VARCHAR2,CLOB,orNCLOB. Ifreplace_string is aCLOB orNCLOB,then Oracle truncatesreplace_string to 32K. Thereplace_string can contain up to 500 backreferences to subexpressions in the form\n,wheren is a number from 1 to 9. Ifn is the backslash character inreplace_string,then you must precede it with the escape character (\\). For more information on backreference expressions,please refer to the notes to"Oracle Regular Expression Support",Table C-1.

  • position is a positive integer indicating the character of source_char where Oracle should begin the search. The default is 1,meaning that Oracle begins the search at the first character ofsource_char.

  • occurrence is a nonnegative integer indicating the occurrence of the replace operation:

    • If you specify 0,then Oracle replaces all occurrences of the match.

    • If you specify a positive integer n,then Oracle replaces the nth occurrence.

  • match_parameter is a text literal that lets you change the default matching behavior of the function. This argument affects only the matching process and has no effect onreplace_string. You can specify one or more of the following values formatch_parameter:

    • 'i' specifies case-insensitive matching.

    • 'c' specifies case-sensitive matching.

    • 'n' allows the period (.),which is the match-any-character character,to match the newline character. If you omit this parameter,the period does not match the newline character.

    • 'm' treats the source string as multiple lines. Oracle interprets^ and$ as the start and end,respectively,of any line anywhere in the source string,rather than only at the start or end of the entire source string. If you omit this parameter,Oracle treats the source string as a single line.

    • 'x' ignores whitespace characters. By default,whitespace characters match themselves.

    If you specify multiple contradictory values,Oracle uses the last value. For example,if you specify'ic',then Oracle uses case-sensitive matching. If you specify a character other than those shown above,then Oracle returns an error.

    If you omit match_parameter,then:

    • The default case sensitivity is determined by the value of the NLS_SORT parameter.

    • A period (.) does not match the newline character.

    • The source string is treated as a single line.



Examples

The following example examines phone_number,looking for the patternxxx.xxx.xxxx. Oracle reformats this pattern with (xxx)xxx-xxxx.



  1. SELECT
  2. REGEXP_REPLACE(phone_number,
  3. '([[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4})',
  4. '(\1)\2-\3')"REGEXP_REPLACE"
  5. FROMemployees;
  6. REGEXP_REPLACE
  7. --------------------------------------------------------------------------------
  8. (515)123-4567
  9. (515)123-4568
  10. (515)123-4569
  11. (590)423-4567
  12. ...

The following example examines country_name. Oracle puts a space after each non-null character in the string.


  1. SELECT
  2. REGEXP_REPLACE(country_name,'(.)','\1')"REGEXP_REPLACE"
  3. FROMcountries;
  4. REGEXP_REPLACE
  5. --------------------------------------------------------------------------------
  6. Argentina
  7. Australia
  8. Belgium
  9. Brazil
  10. Canada
  11. ...



The following example examines the string,looking for two or more spaces. Oracle replaces each occurrence of two or more spaces with a single space.


  1. SELECT
  2. REGEXP_REPLACE('500OracleParkway,RedwoodShores,CA',
  3. '(){2,'')"REGEXP_REPLACE"
  4. FROMDUAL;
  5. REGEXP_REPLACE
  6. --------------------------------------
  7. 500OracleParkway,CA

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


jquery.validate使用攻略(表单校验) 目录 jquery.validate使用攻略1 第一章 jquery.validate使用攻略1 第二章 jQuery.validate.js API7 Custom selectors7 Utilities8 Validato
/\s+/g和/\s/g的区别 正则表达式/\s+/g和/\s/g,目的均是找出目标字符串中的所有空白字符,但两者到底有什么区别呢? 我们先来看下面一个例子: let name = 'ye wen jun';let ans = name.replace(/\s/g, '&#3
自整理几个jquery.Validate验证正则: 1. 只能输入数字和字母 /^[0-9a-zA-Z]*$/g jQuery.validator.addMethod("letters", function (value, element) { return this.optio
this.optional(element)的用法 this.optional(element)是jquery.validator.js表单验证框架中的一个函数,用于表单控件的值不为空时才触发验证。 简单来说,就是当表单控件值为空的时候不会进行表单校验,此函数会返回true,表示校验通过,当表单控件
jQuery.validate 表单动态验证 实际上jQuery.validate提供了动态校验的方法。而动态拼JSON串的方式是不支持动态校验的。牺牲jQuery.validate的性能优化可以实现(jQuery.validate的性能优化见图1.2 jQuery.validate源码 )。 也可
自定义验证之这能输入数字(包括小数 负数 ) <script type="text/javascript"> function onlyNumber(obj){ //得到第一个字符是否为负号 var t = obj.value.charAt(0); //先把非数字的都
// 引入了外部的验证规则 import { validateAccountNumber } from "@/utils/validate"; validator.js /*是否合法IP地址*/ export function validateIP(rule, value,cal
VUE开发--表单验证(六十三) 一、常用验证方式 vue 中表单字段验证的写法和方式有多种,常用的验证方式有3种: data 中验证 表单内容: <!-- 表单 --> <el-form ref="rulesForm" :rules="formRul
正则表达式 座机的: 例子: 座机有效写法: 0316-8418331 (010)-67433539 (010)67433539 010-67433539 (0316)-8418331 (0316)8418331 正则表达式写法 0\d{2,3}-\d{7,8}|\(?0\d{2,3}[)-]?\d
var reg = /^0\.[1-9]{0,2}$/;var linka = 0.1;console.log (reg.test (linka)); 0到1两位小数正则 ^(0\.(0[1-9]|[1-9]{1,2}|[1-9]0)$)|^1$ 不含0、0.0、0.00 // 验证是否是[1-10
input最大长度限制问题 <input type="text" maxlength="5" /> //可以 <input type="number" maxlength="5" /> //没有效
js输入验证是否为空、是否为null、是否都是空格 目录 1.截头去尾 trim 2.截头去尾 会去掉开始和结束的空格,类似于trim 3.会去掉所有的空格,包括开始,结束,中间 1.截头去尾 trim str=str.trim(); // 强烈推荐 最常用、最实用 or $.trim(str);
正则表达式语法大全 字符串.match(正则):返回符合的字符串,若不满足返回null 字符串.search(正则):返回搜索到的位置,若非一个字符,则返回第一个字母的下标,若不匹配则返回-1 字符串.replace(正则,新的字符串):找到符合正则的内容并替换 正则.test(字符串):在字符串中
正整数正则表达式正数的正则表达式(包括0,小数保留两位): ^((0{1}.\d{1,2})|([1-9]\d.{1}\d{1,2})|([1-9]+\d)|0)$正数的正则表达式(不包括0,小数保留两位): ^((0{1}.\d{1,2})|([1-9]\d.{1}\d{1,2})|([1-9]+
JS 正则验证 test() /*用途:检查输入手机号码是否正确输入:s:字符串返回:如果通过验证返回true,否则返回false /function checkMobile(s){var regu =/[1][3][0-9]{9}$/;var re = new RegExp(regu);if (r
请输入保留两位小数的销售价的正则: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/ 1.只能输入英文 <input type="text" onkeyup="value
判断价格的正则表达式 价格的正则表达式 /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/; 1 解析:价格符合两种格式 ^ [1-9]\d*(.\d{1,2})?$ : 1-9 开头,后跟是 0-9,可以跟小数点,但小数点后要带上 1-2 位小数,类似 2,2
文章浏览阅读106次。这篇文章主要介绍了最实用的正则表达式整理,比如校验邮箱的正则,号码相关,数字相关等等,本文给大家列举的比较多,需要的朋友可以参考下。_/^(?:[1-9]d*)$/ 手机号
文章浏览阅读1.2k次。4、匹配中的==、an==、== an9、i9 == "9i"和99p==请注意下面这部分的作用,它在匹配中间内容的时候排除了说明:当html字符串如下时,可以匹配到两处,表示匹配的字符串不包含and且不包含空白字符。说明:在上面的正则表达式中,_gvim正则表达式匹配不包含某个字符串
文章浏览阅读897次。【代码】正则表达式匹配a标签的href。_auto.js 正则匹配herf