Use the Regular Expression Function REGEXP_LIKE in Oracle to query

Source: Internet
Author: User

The Like operators in Oracle use '_' and '%' As wildcards, just Like this:

SELECT name FROM test_like WHERE name like'_ A %';

That is, all rows with the 2nd letters a in the name column of the test_like table are matched. However, note that Oracle matches the time zone in Case sensitivity. That is to say, the line name = 'saas 'cannot be queried during the above query.

The regular expression function provided by Oracle10g can solve this problem well. Of course, this is not the only advantage of using a regular expression function. In fact, it is much more powerful than the Like operator.

The syntax of regular expressions is unnecessary. Currently, most languages support regular expressions.

The following describes how to use the regular expression function REGEXP_LIKE in Oracle:

  1. REGEXP_LIKE (x, pattern [, match_option])
  2. If the source string x matches the regular expression patternTrue. You can use match_option to modify the default matching option. This parameter can be set:
  3. -'C', Indicating that the matching time zone is case sensitive (default option)
  4. -'I', Indicating that the matching is case insensitive.
  5. -'N'Allows the use of operators that can match any character (usually'.')
  6. -'M', Uses x as a string that contains multiple rows

For example:

SELECT*FROMTest_regWHEREREGEXP_LIKE (Name,'(A) \ 1','I');

The preceding SQL statement matches the name column in The test_reg table with two consecutive characters 'A' (Case Insensitive), such as name = 'saas '. In addition, we also use the back reference syntax in the Regular Expression -- \ n to repeat the content of the last match n times. Here () \ 1 indicates matching two consecutive characters 'A '.

Note that the double brackets must be used for subsequent references. Otherwise, the following result is displayed:

  1. SELECT*FROMTest_regWHEREREGEXP_LIKE (Name,'A \ 1','I')
  2. ORA-12727: the backward reference in the regular expression is invalid

Finally, do not confuse the wildcards of the like operator with the regular expression syntax. That is to say, do not use wildcards in the LIKE operator in a regular expression. If this is done, unknown results will be obtained, because '_' and '%' are matched by regular expressions as common characters.

For example, if the following SQL statement is used to obtain the record name = 'saas ', the actual query result is null.

  1. SQL>SELECT*FROMTest_regWHEREREGEXP_LIKE (Name,'^ _ (A) \ 1','I');
  2. NAME
  3. ----------

Actually use:

  1. SQL>SELECT*FROMTest_regWHEREREGEXP_LIKE (Name,'^. (A) \ 1','I');
  2. NAME
  3. ----------
  4. SaAs

For more information about Oracle, see Oracle topics page http://www.bkjia.com/topicnews.aspx? Tid = 12

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.