Oracle regexp_like knowledge and Examples

Source: Internet
Author: User

Oracle regexp_likeThe knowledge and examples are the main content we will introduce in this article. First, let's take a look at the Oracle SupportRegular ExpressionMainly include the following four functions:

1. REGEXP_LIKE: similar to LIKE.

2. REGEXP_INSTR: similar to INSTR.

3. REGEXP_SUBSTR: similar to SUBSTR.

4. REGEXP_REPLACE: similar to REPLACE.

They are used in the same way as Oracle SQL functions LIKE, INSTR, SUBSTR, and REPLACE, but they use POSIX Regular Expressions instead of the old percent signs %) and wildcard characters.

POSIX regular expressions are composed of standard metacharacters. '$' matches the end position of the input string. If the Multiline attribute of the RegExp object is set, $ also matches '\ n' or' \ R '. '? 'Match the previous subexpression zero or once. '*' Matches the previous subexpression zero or multiple times.

'|' Indicates an option between the two items. Example '^ ([a-z] + | [0-9] +) $' indicates the combination of all lowercase letters or numbers '() 'indicates the start and end positions of a subexpression. '{M, n}' indicates the exact number of occurrences. m = <number of occurrences <= n, '{m}' indicates m occurrences, '{m ,} 'indicates that at least m occurs.

\ Num matches num, where num is a positive integer. References to the obtained matching. [[: Alpha:] any letter.

[[: Digit:] any number.

[[: Alnum:] Any letter or number.

[[: Space:] any white characters.

[[: Upper:] Any uppercase letter.

[[: Lower:] Any lowercase letter.

[[: Punct:] Any punctuation marks.

[[: Xdigit:] Any hexadecimal number, which is equivalent to [0-9a-fA-F]. \ Escape Character *, + ,?, {N}, {n ,}, {n, m} qualifier ^, $, anymetacharacter location and order.

 
 
  1. Create table fzq id varchar (4 ),
  2. Value varchar (10) -- data insertion
  3. Insert into fzq values
  4. ('1', '123 ');
  5. Insert into fzq values
  6. ('2', '123 ');
  7. Insert into fzq values
  8. ('3', '1b3b560 ');
  9. Insert into fzq values
  10. ('4', 'abc ');
  11. Insert into fzq values
  12. ('5', 'abcde ');
  13. Insert into fzq values
  14. ('6', 'adreasx ');
  15. Insert into fzq values
  16. ('7', '2014, 123 45 ');
  17. Insert into fzq values
  18. ('8', 'adc de ');
  19. Insert into fzq values
  20. ('9', 'adc,. de ');
  21. Insert into fzq values
  22. ('10', '1b ');
  23. Insert into fzq values
  24. ('10', 'abcbvbnb ');
  25. Insert into fzq values
  26. ('11', '123 ');
  27. Insert into fzq values
  28. ('11', '123 ');

-- Regexp_like

-- Query records whose names start with 60 and end with 1 and whose length is 7 characters.

 
 
  1. select * from fzq where value like '1____60';  
  2. select * from fzq where regexp_like(value,'1....60'); 

-- Query records whose names start with 60 and end with 1 and whose length is 7 digits and all are numbers.

-- Using like is not a good implementation.

 
 
  1. select * from fzq where regexp_like(value,'1[0-9]{4}60');   
  2. select * from fzq where regexp_like(value,'1[[:digit:]]{4}60'); 

-- Query records whose values are not pure numbers

Select * from fzq where not regexp_like (value, '^ [[: digit:] + $ ');

-- Query records whose values do not contain any numbers.

Select * from fzq where regexp_like (value, '^ [^ [: digit:] + $ ');

-- Query records starting with 12 or 1b. The records are case insensitive.

Select * from fzq where regexp_like (value, '^ 1 [2b]', 'I ');

-- Query records starting with 12 or 1b. case sensitive.

 
 
  1. select * from fzq where regexp_like(value,'^1[2B]'); select * from fzq where regexp_like(value,'[[:space:]]');   
  2. select * from fzq where regexp_like(value,'^([a-z]+|[0-9]+)$'); select * from fzq where regexp_like(value,'[[:punct:]]'); 

Example: judge whether the name is null. It contains less than two characters, including numbers and letters.

 
 
  1. Create or replace
  2. FUNCTION CheckName (NameStr in VARCHAR2) RETURN integer
  3. As
  4. BEGIN
  5. -- Returns 1 If yes, and returns 0 if no
  6. If (NameStr is null or length (NameStr) <2) then
  7. Return 0;
  8. Else
  9. If (NameStr like '% not named %') then
  10. RETURN 0;
  11. End if;
  12. If regexp_like (NameStr, '^ ([a-z] + | [0-9] + | [A-Z] +) $') then
  13. Return 0;
  14. End if;
  15. Return 1;
  16. End if;
  17. END CheckName;

This section describes the knowledge of Oracle regexp_like and examples. We hope this introduction will be helpful to you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.