--18 SQL statement, the regular expression functions

Oracle Regular Expressions (regularexpression)

Metacharacters Character Meaning for example
^ Matches the start position of the string (in [], in which case it does not accept the set of characters represented.
^ A: the start of a string matching
[^ A]: matches a string of free
-
When used in am indicates a range;
When expressed in the first character
Even string such as [-ABC]
 
$ Matching characters end position 'A $': a matching string to the end of the
. Matches any one of the n characters except a newline.  
? Matches the preceding subexpression zero or one tr (y (ing)?): may be expressed or try trying
* Matches the preceding subexpression zero or more times  
+ Matches the preceding subexpression one or more times  
() Mark the start and end position of a sub-expressions
a (b) * matches
ab, abb, abbb, youabb
(C | d) matching c or d
[] Mark a bracket expression
[Cd] match selection equivalent to c or d
(C | d). It matches a single character, [^ cd] match c, other than single character. D.
[Az] means all lowercase letters
{m,n} m = <Occurrence <= n, '{m}' represents appear m times, '{m,}' indicates that an at least m times.  
| Representation or relationship. Indicate a choice between the two  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Character cluster Character Meaning
[[:alpha:]] Any letter
[[:digit:]] Any number
[[Charon]] Any letters and numbers
[[:space:]] Any whitespace characters
[[:upper:]] Any capital letters
[[:lower:]] Any lowercase letters
[[:point:]] Any punctuation
[[Xdigit]] Any hexadecimal number, is equivalent to [0-9a-fA-F]

 

 

 

 

 

 

 

 

regexp_like('S','RE','O')

  • The search string or the S column expression
  • RE regular content
  • O parameters (optional)
idle> select ename from emp where regexp_like(ename,'S');

ENAME
----------
SMITH
JONES
SCOTT
ADAMS
JAMES

idle> select ename from emp where regexp_like(ename,'^S.*T');

ENAME
----------
SMITH
SCOTT

idle>

idle> idle> select ename from emp whereREGEXP_LIKE (ename, ' . ^ S * T $ ' , ' I ' ); Ignore case 

the ENAME 
- -------- 
SCOTT

 

regexp_replace(S,'RE','NEW')

idle> select ename,regexp_replace(ename,'S..T','=') aaa from emp where regexp_like (ename,'S..T');

ENAME   AAA
---------- ----------
SMITH   =H
SCOTT   =T

Complex usage

SELECT REGEXP_REPLACE(
'Ellen Hildi Smith',
'(.*) (.*) (.*)', '\3, \1 \2')
FROM dual

REGEXP_REPLACE('EL
------------------
Smith, Ellen Hildi 

The SQL statement shows three separate sub-expressions enclosed in parentheses. Each individual sub-expression contains a matching metacharacter (.), And followed by * metacharacters that any character (except a newline) must be matched zero or more times. Spaces each separate sub-expressions, spaces must also match. Creating value acquisition sub-expressions in parentheses, and can \ digit referenced. First subexpression is assigned to \ 1, the second \ 2, and so on. These are arranged after they are used in reference to the last parameter of the function (\ 3 \ 1 \ 2), this function effectively returns the substring Alternatively, press a desired format (including spaces and commas). Table 11 details the various components of the regular expression. 

REGEXP_COUNT

In Oracle 11g version of the introduction, use this function to count the number of occurrences.

REGEXP_COUNT (source_char, pattern [, position [, match_param]])

source_char: the string to the regular process

pattern: regular expression to match

position: starting position, beginning from the first few characters of the regular expression matching (default is 1)

match_param: mode ( 'I' for case-insensitive match

                  'C' for the case-sensitive match

                  'N' to allow a period (.) As a wildcard character to match a newline. If omitted, the sentences point not newline

                  'M' will be considered a multi-line source string. That Oracle will ^ and $ are seen as the source string anywhere in the beginning and end of any line, and not merely as the entire source string begins or ends. If omitted, the source string is regarded as Oracle row.

                  'X' to ignore the space character. By default, the space character and its own match.

                  If you specify multiple values ​​contradictory to match_param, then Oracle uses the last values.

 

                 )

REGEXP_SUBSTR

function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)

  • String : the string to a regular treatment
  • pattern: regular expression to match
  • position: starting position, beginning from the first few characters of the regular expression matching (default is 1)
  • occurrence: identification of several matched set, by default. 1 modifier: mode ( 'i' case insensitive searching, 'c' for a case-sensitive search, the default is 'c')

Example 1: Query using regular first value divided, i.e. 17

SELECT REGEXP_SUBSTR('17,20,23','[^,]+',1,1,'i') AS STR FROM DUAL;  

STR
-----
17

 

Example 2: a value after the last query using a regular division, i.e. 23

SELECT REGEXP_SUBSTR('17,20,23','[^,]+',1,3,'i') AS STR FROM DUAL;  

STR
----
23

Example 3: string split rows

select regexp_substr('17,20,23','[^,]+',1,level)  AS STR
  from dual
connect
by level<=regexp_count('17,20,23',',')+1 STR ---- 17 20 23

Example 4: When the table (rows of data) as a data source, a field conversion table for the rows, there will Cartesian product.

SELECT lpad('~',(LEVEL-1)*5,'~')||CKJYM1 CKJYM1,LEVEL,regexp_substr(CKJYM1,'[^,]+',1,level) CKJYM
              FROM (SELECT 'B10,B04' CKJYM1
                      FROM dual
                      union all
                     SELECT 'B11,B12' CKJYM1
                      FROM dual ) T
            CONNECT BY LEVEL <= REGEXP_COUNT(CKJYM1,'B')  

 CKJYM1                      LEVEL         CKJYM
---------------------   -------------   ----------
B10,B04                       1              B10
~~~~~B10,B04                  2              B04
~~~~~B11,B12                  2              B12
B11,B12                       1              B11
~~~~~B10,B04                  2              B04
~~~~~B11,B12                  2              B12

CONNECT BY is equivalent to a recursive self-ligation, each connected continuously to the result of superposition to the result set. And recursive outlet connection conditions between the two layers written in the CONNECT BY.

Here the data is no parent-child relationship, just to make repeated the same line data. So our conditions connected only used the table's primary key CKJYM1 = PRIOR CKJYM1, in addition LEVEL control layers and then export as recursion, you can get the desired data. ORACLE but there is a check, if you have a before and after the join condition (CKJYM1 = PRIOR CKJYM1), when the same data row again, it will report a error: ORA-01 436: the CONNECT BY Loop the Data in the User

To deceive it, here with a DBMS_RANDOM.VALU PRIOR E (equivalent to acquiring a random number ) , because DBMS_RANDOM.VALUE Each call returns a different result, it believed two rows of data is not the same, it is not being given.

So sql should read:

SELECT lpad('~',(LEVEL-1)*5,'~')||CKJYM1 CKJYM1,LEVEL,regexp_substr(CKJYM1,'[^,]+',1,level) CKJYM
              FROM (SELECT 'B10,B04' CKJYM1
                      FROM dual
                      union all
                     SELECT 'B11,B12' CKJYM1
                      FROM dual ) T
            CONNECT BY LEVEL <=
                       REGEXP_COUNT(CKJYM1,'B')  
                       and CKJYM1=PRIOR CKJYM1
                       and prior dbms_random.value is not null

CKJYM1 LEVEL CKJYM
------------ -------- ---------- B10,B04 1 B10 ~~~~~B10,B04 2 B04 B11,B12 1 B11 ~~~~~B11,B12 2 B12

Guess you like

Origin www.cnblogs.com/marxist/p/12151189.html