SlideShare ist ein Scribd-Unternehmen logo
1 von 38
Regular Expressions in Oracle

     Logan Palanisamy
Agenda

 Introduction to regular expressions
 REGEXP_* functions in Oracle
 Coffee Break
 Examples
 More examples
Meeting Basics

 Put your phones/pagers on vibrate/mute
 Messenger: Change the status to offline or
  in-meeting
 Remote attendees: Mute yourself (*6). Ask
  questions via Adobe Connect.
What are Regular Expressions?

 A way to express patterns
   credit cards, license plate numbers, vehicle identification
    numbers, voter id, driving license
 UNIX (grep, egrep), PHP, JAVA support Regular
  Expressions
 PERL made it popular
String operations before Regular Expression
               support in Oracle

 Pull the data from DB and perform it in middle tier
  or FE
 OWA_PATTERN in 9i and before
 LIKE operator
LIKE operator

 % matches zero or more of any character
 _ matches exactly one character
 Examples
    WHERE col1 LIKE 'abc%';
    WHERE col1 LIKE '%abc';
    WHERE col1 LIKE 'ab_d';
    WHERE col1 LIKE 'ab_d' escape '';
    WHERE col1 NOT LIKE 'abc%';
 Very limited functionality
    Check whether first character is numeric: where c1 like '0%' OR c1
     like '1%' OR .. .. c1 like '9%'
    Very trivial with Regular Exp: where regexp_like(c1, '^[0-9]')
Regular Expressions

Meta        Meaning
character
.           Matches any single "character" except newline.
*           Matches zero or more of the character preceding it
            e.g.: bugs*, table.*
^           Denotes the beginning of the line. ^A denotes lines starting
            with A
$           Denotes the end of the line. :$ denotes lines ending with :
           Escape character (., *, [, , etc)
[]          matches one or more characters within the brackets. e.g.
            [aeiou], [a-z], [a-zA-Z], [0-9], [:alpha:], [a-z?,!]
[^]         negation - matches any characters other than the ones
            inside brackets. eg. ^[^13579] denotes all lines not starting
            with odd numbers, [^02468]$ denotes all lines not ending
            with even numbers
                                                                   7
Extended Regular Expressions

Meta character   Meaning
|                alternation. e.g.: ho(use|me), the(y|m),
                 (they|them)
+                one or more occurrences of previous character.
?                zero or one occurrences of previous character.
{n}              exactly n repetitions of the previous char or group
{n,}             n or more repetitions of the previous char or
                 group
{,m}             zero to m repetitions of the previous char or
                 group
{n, m}           n to m repetitions of previous char or group
(....)           grouping or subexpression
n               back referencing where n stands for the nth sub-
                 expression. e.g.: 1 is the back reference for first
                 sub-expression.
                                                                 8
POSIX Character Classes

POSIX         Description
[:alnum:]     Alphanumeric characters
[:alpha:]     Alphabetic characters
[:ascii:]     ASCII characters
[:blank:]     Space and tab
[:cntrl:]     Control characters
[:digit:]     Digits, Hexadecimal digits
[:xdigit:]
[:graph:]     Visible characters (i.e. anything except spaces, control characters,
              etc.)
[:lower:]     Lowercase letters

[:print:]     Visible characters and spaces (i.e. anything except control
              characters)
[:punct:]     Punctuation and symbols.
[:space:]     All whitespace characters, including line breaks
[:upper:]     Uppercase letters
[:word:]      Word characters (letters, numbers and underscores)
Perl Character Classes

Perl   POSIX           Description
d     [[:digit:]]     [0-9]
D     [^[:digit:]]    [^0-9]
w     [[:alnum:]_]    [0-9a-zA-Z_]
W     [^[:alnum:]_]   [^0-9a-zA-Z_]
s     [[:space:]]
S     [^[:space:]]




                                         10
Tools to learn Regular Expressions

 http://www.weitz.de/regex-coach/
 http://www.regexbuddy.com/
REGEXP_* functions

 Available from 10g onwards.
 Powerful and flexible, but CPU-hungry.
 Easy and elegant, but sometimes less performant
 Usable on text literal, bind variable, or any column
  that holds character data such as CHAR, NCHAR,
  CLOB, NCLOB, NVARCHAR2, and VARCHAR2
  (but not LONG).
 Useful as column constraint for data validation
REGEXP_LIKE

 Determines whether pattern matches.
 REGEXP_LIKE (source_str, pattern,
  [,match_parameter])
 Returns TRUE or FALSE.
 Use in WHERE clause to return rows matching a pattern
 Use as a constraint
    alter table t add constraint alphanum check (regexp_like (x,
     '[[:alnum:]]'));
 Use in PL/SQL to return a boolean.
    IF (REGEXP_LIKE(v_name, '[[:alnum:]]')) THEN ..
 Can't be used in SELECT clause
 regexp_like.sql
REGEXP_SUBSTR

 Extracts the matching pattern. Returns NULL when
    nothing matches
   REGEXP_SUBSTR(source_str, pattern [, position
    [, occurrence [, match_parameter]]])
   position: character at which to begin the search.
    Default is 1
   occurrence: The occurrence of pattern you want to
    extract
   regexp_substr.sql
REGEXP_INSTR

 Returns the location of match in a string
 REGEXP_INSTR(source_str, pattern, [, position [,
  occurrence [, return_option [, match_parameter]]]])
 return_option:
    0, the default, returns the position of the first character.
    1 returns the position of the character following the occurence.
 regexp_instr.sql
REGEXP_REPLACE

 Search and Replace a pattern
 REGEXP_REPLACE(source_str, pattern
  [, replace_str] [, position [, occurrence
  [, match_parameter]]]])
 If replace_str is not specified, pattern/search_str is
  replaced with empty string
 occurence:
    when 0, the default, replaces all occurrences of the match.
    when n, any positive integer, replaces the nth occurrence.
 regexp_replace.sql
REGEXP_COUNT

 New in 11g
 Returns the number of times a pattern appears in a
  string.
 REGEXP_COUNT(source_str, pattern [,position
  [,match_param]])
 For simple patterns it is same as
  (LENGTH(source_str) –
  LENGTH(REPLACE(source_str,
  pattern)))/LENGTH(pattern)
 regexp_count.sql
Pattern Matching modifiers

 i – Specifies case-insensitive matching (ignore case)
 c – Specifies case-sensitive matching
 n – allows the period (.) to match the newline character
 m - treats the source string as multiple lines.
 x - ignores whitespace characters
 when match_parameter is not specified,
     case sensitivity is determined by NLS_SORT parameter
      (BINARY, BINARY_CI)
     A period (.) doesn't match newline character
     Source string is treated as a single line
 match_params.sql
Is a CHAR column all numeric?

 to_number(c1) returns ORA-01722:
  invalid number if a varchar2 column
  contains alpha characters.
 is_numeric.sql
Check constraints

 Put validation close to where the data is
  stored
 No need to have validation at different
  clients
 check_constraint.sql
Extract email-ids

 Find email-ids embedded in text
  strings. Possible email-id formats:
  abc123@company.com

  namex@mail.company.com

  xyz_1@yahoo.co.in

 extract_emailid.sql
Extract dates

 Extract dates embedded in text strings.
 Possible formats
 1/5/2007, 2-5-03, 12-31-2009,
  1/31/10, 2/5-10

 extract_date.sql
Extracting hostnames from URLs
 Extract hostnames/domain-names embedded in
 text strings. Possible formats
  http://us.mg201.mail.yahoo.com/dc/launch?.partner
   =sbc&.gx=1&.rand=fegr2vucbecu5
  https://www.mybank.com:8080/abc/xyz

  www.mybank.com

  ftp://www.mycharity.org/abc/xyz

 extract_hostname.sql
Convert value pairs to XML
 Input: A string such as 'remain1=1;remain2=2;'
 Output: An XML string
  <remain1><value=1></remain1>
  <remain2><value=2></remain2>
 convert_to_xml.sql
Sort IP addresses in numerical order
 Sort IP addresses, that are stored as character
  strings, in numerical order.
 Input
    10.10.20.10
    127.0.0.1
    166.22.33.44
    187.222.121.0
    20.23.23.20

 sort_ip_address.sql
Extract first name, last name, and middle initial

 Extract the first name, last name with
  an optional middle initial.
 first_last_mi.sql
Finding the Last Occurrence

 Find the last numeric sequence from a
  sequence.
 Return 567 from 'abc/123/def567/xyz'
 INSTR and SUBSTR allow backward
  search when position is negative.
  REGEXP functions don't allow backward
  search
 last_occurrence.sql
Fuzzy Match

 Tables t1 and t2 each have a
  varchar2(12) column (t1.x, t2.y).
 A row in t1 is considered a match for a
  row in t2, if any six characters in t1.x
  matches with any six characters in t2.y
 fuzzy_match.sql
The lazy operator

 ? is lazy/non-greedy quantifier
 greedy_lazy.sql
Meta-characters with multiple meanings

 Same meta characters are used with
  multiple meanings
   ^ used for anchoring and negation.

   ? used as quantifier and lazy operator

   () used for grouping or sub-expression

 metachars_with_multiple_meanings.sql
Nuances

 ? (zero or one), * (zero or more)
  could sometimes mislead you
 nuances.sql
Stored patterns


 patterns can be stored in table
  columns and be referenced in
  REGEXP functions
 No need to hard-code them

 stored_patterns.sql
Random things
 Insert a dash before the two last digits
 Remove a substring
 Get rid of useless commas from a string
 Find the word that comes immediately
  before a substring (e.g. XXX)
 Replace whole words, not its parts
 Trimming the trailing digits
 random.sql
A few other points

 When not to use Regular Expressions
   If the same thing could be used without regular
    expressions and without too much coding.
 POSIX notations need double brackets [[:upper]]. [:upper:]
  won't work. [[:UPPER:]] won't work either. It has to be in
  lower case letters.
 Locale support provided with Collation Element ][.ce.]],
  and Equivalence Classes [[=e=]]
 MySQL supports regular expressions with RLIKE
References

 Oracle® Database Advanced Application
    Developer's Guide
    (http://download.oracle.com/docs/cd/E11882_0
    1/appdev.112/e17125/adfns_regexp.htm#CHDGH
    BHF)
   Anti-Patterns in Regular Expressions:
    http://gennick.com/antiregex.html
   First Expressions. An article by Jonathan Gennick Oracle
    Magazine, Sep/Oct 2003.
   Oracle Regular Expressions Pocket Reference by
    Gonathan Gennick
   http://examples.oreilly.com/9780596006013/Re
    gexPocketData.sql
References ...

 http://www.psoug.org/reference/regexp.html
 http://download.oracle.com/docs/cd/E11882_01/se
  rver.112/e10592/conditions007.htm#SQLRF00501
 http://www.oracle.com/technology/pub/articles/sat
  ernos_regexp.html
 http://www.oracle.com/technology/products/datab
  ase/application_development/pdf/TWP_Regular_E
  xpressions.pdf
 http://asktom.oracle.com/pls/asktom/asktom.searc
  h?p_string=regexp_
References ...

 http://www.oracle.com/technology/obe/10gr2_db_single/de
    velop/regexp/regexp_otn.htm
   http://www.oracle.com/technology/sample_code/tech/pl_sq
    l/index.html
   http://forums.oracle.com/forums/thread.jspa?threadID=427
    716
   http://forums.oracle.com/forums/search.jspa?threadID=&q=
    regular+expression&objID=f75&dateRange=all&userID=&nu
    mResults=120&rankBy=9
   http://www.oracle.com/technology/sample_code/tech/pl_sq
    l/index.html
   http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUE
    STION_ID:2200894550208#1568589800346862515
Q&A

 devel_oracle@

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Regular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular ExpressionsRegular Expressions 101 Introduction to Regular Expressions
Regular Expressions 101 Introduction to Regular Expressions
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Data manipulation language
Data manipulation languageData manipulation language
Data manipulation language
 
[APJ] Common Table Expressions (CTEs) in SQL
[APJ] Common Table Expressions (CTEs) in SQL[APJ] Common Table Expressions (CTEs) in SQL
[APJ] Common Table Expressions (CTEs) in SQL
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
DML Commands
DML CommandsDML Commands
DML Commands
 
SQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaSQL Joins With Examples | Edureka
SQL Joins With Examples | Edureka
 
Mysql
MysqlMysql
Mysql
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?
 
Sql - Structured Query Language
Sql - Structured Query LanguageSql - Structured Query Language
Sql - Structured Query Language
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
basic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptxbasic structure of SQL FINAL.pptx
basic structure of SQL FINAL.pptx
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Oop java
Oop javaOop java
Oop java
 
Subqueries
SubqueriesSubqueries
Subqueries
 
Difference Between Sql - MySql and Oracle
Difference Between Sql - MySql and OracleDifference Between Sql - MySql and Oracle
Difference Between Sql - MySql and Oracle
 
Understanding linq
Understanding linqUnderstanding linq
Understanding linq
 

Andere mochten auch

Oracle regular expressions
Oracle regular expressionsOracle regular expressions
Oracle regular expressionsudaya1988
 
Aprende html efectivo - Resumen Capítulo 1
Aprende html efectivo - Resumen Capítulo 1Aprende html efectivo - Resumen Capítulo 1
Aprende html efectivo - Resumen Capítulo 1Juanjo Bote
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 
3 Steps to Fix Your Customer Support Strategy
3 Steps to Fix Your Customer Support Strategy3 Steps to Fix Your Customer Support Strategy
3 Steps to Fix Your Customer Support StrategyLogMeIn
 
Software utilitario presentacion
Software utilitario presentacionSoftware utilitario presentacion
Software utilitario presentacionJavierReyesCastillo
 
Software de aplicación Programas utilitarios
Software de aplicación   Programas utilitariosSoftware de aplicación   Programas utilitarios
Software de aplicación Programas utilitariosJuan Drt
 
Paginas de matematicas
Paginas de matematicasPaginas de matematicas
Paginas de matematicasespanol
 

Andere mochten auch (14)

Oracle regular expressions
Oracle regular expressionsOracle regular expressions
Oracle regular expressions
 
Aprende html efectivo - Resumen Capítulo 1
Aprende html efectivo - Resumen Capítulo 1Aprende html efectivo - Resumen Capítulo 1
Aprende html efectivo - Resumen Capítulo 1
 
Presentacion de dropbox
Presentacion de dropboxPresentacion de dropbox
Presentacion de dropbox
 
Resumen de DropBox
Resumen de DropBoxResumen de DropBox
Resumen de DropBox
 
Bio2#8
Bio2#8Bio2#8
Bio2#8
 
Logmein presentación
Logmein presentaciónLogmein presentación
Logmein presentación
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
File000173
File000173File000173
File000173
 
3 Steps to Fix Your Customer Support Strategy
3 Steps to Fix Your Customer Support Strategy3 Steps to Fix Your Customer Support Strategy
3 Steps to Fix Your Customer Support Strategy
 
Presentación dropbox
Presentación dropboxPresentación dropbox
Presentación dropbox
 
Utilitarios
UtilitariosUtilitarios
Utilitarios
 
Software utilitario presentacion
Software utilitario presentacionSoftware utilitario presentacion
Software utilitario presentacion
 
Software de aplicación Programas utilitarios
Software de aplicación   Programas utilitariosSoftware de aplicación   Programas utilitarios
Software de aplicación Programas utilitarios
 
Paginas de matematicas
Paginas de matematicasPaginas de matematicas
Paginas de matematicas
 

Ähnlich wie Regular expressions in oracle

SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)Logan Palanisamy
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular ExpressionsMukesh Tekwani
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
Regular expression
Regular expressionRegular expression
Regular expressionRajon
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARDTia Ricci
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and stringsRai University
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in rDr Nisha Arora
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsRai University
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsRai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-aneebkmct
 

Ähnlich wie Regular expressions in oracle (20)

SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)SQL for pattern matching (Oracle 12c)
SQL for pattern matching (Oracle 12c)
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 
Les08
Les08Les08
Les08
 
A regex ekon16
A regex ekon16A regex ekon16
A regex ekon16
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Regular expression
Regular expressionRegular expression
Regular expression
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in r
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 

Kürzlich hochgeladen

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 

Kürzlich hochgeladen (20)

WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 

Regular expressions in oracle

  • 1. Regular Expressions in Oracle Logan Palanisamy
  • 2. Agenda  Introduction to regular expressions  REGEXP_* functions in Oracle  Coffee Break  Examples  More examples
  • 3. Meeting Basics  Put your phones/pagers on vibrate/mute  Messenger: Change the status to offline or in-meeting  Remote attendees: Mute yourself (*6). Ask questions via Adobe Connect.
  • 4. What are Regular Expressions?  A way to express patterns  credit cards, license plate numbers, vehicle identification numbers, voter id, driving license  UNIX (grep, egrep), PHP, JAVA support Regular Expressions  PERL made it popular
  • 5. String operations before Regular Expression support in Oracle  Pull the data from DB and perform it in middle tier or FE  OWA_PATTERN in 9i and before  LIKE operator
  • 6. LIKE operator  % matches zero or more of any character  _ matches exactly one character  Examples  WHERE col1 LIKE 'abc%';  WHERE col1 LIKE '%abc';  WHERE col1 LIKE 'ab_d';  WHERE col1 LIKE 'ab_d' escape '';  WHERE col1 NOT LIKE 'abc%';  Very limited functionality  Check whether first character is numeric: where c1 like '0%' OR c1 like '1%' OR .. .. c1 like '9%'  Very trivial with Regular Exp: where regexp_like(c1, '^[0-9]')
  • 7. Regular Expressions Meta Meaning character . Matches any single "character" except newline. * Matches zero or more of the character preceding it e.g.: bugs*, table.* ^ Denotes the beginning of the line. ^A denotes lines starting with A $ Denotes the end of the line. :$ denotes lines ending with : Escape character (., *, [, , etc) [] matches one or more characters within the brackets. e.g. [aeiou], [a-z], [a-zA-Z], [0-9], [:alpha:], [a-z?,!] [^] negation - matches any characters other than the ones inside brackets. eg. ^[^13579] denotes all lines not starting with odd numbers, [^02468]$ denotes all lines not ending with even numbers 7
  • 8. Extended Regular Expressions Meta character Meaning | alternation. e.g.: ho(use|me), the(y|m), (they|them) + one or more occurrences of previous character. ? zero or one occurrences of previous character. {n} exactly n repetitions of the previous char or group {n,} n or more repetitions of the previous char or group {,m} zero to m repetitions of the previous char or group {n, m} n to m repetitions of previous char or group (....) grouping or subexpression n back referencing where n stands for the nth sub- expression. e.g.: 1 is the back reference for first sub-expression. 8
  • 9. POSIX Character Classes POSIX Description [:alnum:] Alphanumeric characters [:alpha:] Alphabetic characters [:ascii:] ASCII characters [:blank:] Space and tab [:cntrl:] Control characters [:digit:] Digits, Hexadecimal digits [:xdigit:] [:graph:] Visible characters (i.e. anything except spaces, control characters, etc.) [:lower:] Lowercase letters [:print:] Visible characters and spaces (i.e. anything except control characters) [:punct:] Punctuation and symbols. [:space:] All whitespace characters, including line breaks [:upper:] Uppercase letters [:word:] Word characters (letters, numbers and underscores)
  • 10. Perl Character Classes Perl POSIX Description d [[:digit:]] [0-9] D [^[:digit:]] [^0-9] w [[:alnum:]_] [0-9a-zA-Z_] W [^[:alnum:]_] [^0-9a-zA-Z_] s [[:space:]] S [^[:space:]] 10
  • 11. Tools to learn Regular Expressions  http://www.weitz.de/regex-coach/  http://www.regexbuddy.com/
  • 12. REGEXP_* functions  Available from 10g onwards.  Powerful and flexible, but CPU-hungry.  Easy and elegant, but sometimes less performant  Usable on text literal, bind variable, or any column that holds character data such as CHAR, NCHAR, CLOB, NCLOB, NVARCHAR2, and VARCHAR2 (but not LONG).  Useful as column constraint for data validation
  • 13. REGEXP_LIKE  Determines whether pattern matches.  REGEXP_LIKE (source_str, pattern, [,match_parameter])  Returns TRUE or FALSE.  Use in WHERE clause to return rows matching a pattern  Use as a constraint  alter table t add constraint alphanum check (regexp_like (x, '[[:alnum:]]'));  Use in PL/SQL to return a boolean.  IF (REGEXP_LIKE(v_name, '[[:alnum:]]')) THEN ..  Can't be used in SELECT clause  regexp_like.sql
  • 14. REGEXP_SUBSTR  Extracts the matching pattern. Returns NULL when nothing matches  REGEXP_SUBSTR(source_str, pattern [, position [, occurrence [, match_parameter]]])  position: character at which to begin the search. Default is 1  occurrence: The occurrence of pattern you want to extract  regexp_substr.sql
  • 15. REGEXP_INSTR  Returns the location of match in a string  REGEXP_INSTR(source_str, pattern, [, position [, occurrence [, return_option [, match_parameter]]]])  return_option:  0, the default, returns the position of the first character.  1 returns the position of the character following the occurence.  regexp_instr.sql
  • 16. REGEXP_REPLACE  Search and Replace a pattern  REGEXP_REPLACE(source_str, pattern [, replace_str] [, position [, occurrence [, match_parameter]]]])  If replace_str is not specified, pattern/search_str is replaced with empty string  occurence:  when 0, the default, replaces all occurrences of the match.  when n, any positive integer, replaces the nth occurrence.  regexp_replace.sql
  • 17. REGEXP_COUNT  New in 11g  Returns the number of times a pattern appears in a string.  REGEXP_COUNT(source_str, pattern [,position [,match_param]])  For simple patterns it is same as (LENGTH(source_str) – LENGTH(REPLACE(source_str, pattern)))/LENGTH(pattern)  regexp_count.sql
  • 18. Pattern Matching modifiers  i – Specifies case-insensitive matching (ignore case)  c – Specifies case-sensitive matching  n – allows the period (.) to match the newline character  m - treats the source string as multiple lines.  x - ignores whitespace characters  when match_parameter is not specified,  case sensitivity is determined by NLS_SORT parameter (BINARY, BINARY_CI)  A period (.) doesn't match newline character  Source string is treated as a single line  match_params.sql
  • 19. Is a CHAR column all numeric?  to_number(c1) returns ORA-01722: invalid number if a varchar2 column contains alpha characters.  is_numeric.sql
  • 20. Check constraints  Put validation close to where the data is stored  No need to have validation at different clients  check_constraint.sql
  • 21. Extract email-ids  Find email-ids embedded in text strings. Possible email-id formats: abc123@company.com namex@mail.company.com xyz_1@yahoo.co.in  extract_emailid.sql
  • 22. Extract dates  Extract dates embedded in text strings. Possible formats 1/5/2007, 2-5-03, 12-31-2009, 1/31/10, 2/5-10  extract_date.sql
  • 23. Extracting hostnames from URLs  Extract hostnames/domain-names embedded in text strings. Possible formats  http://us.mg201.mail.yahoo.com/dc/launch?.partner =sbc&.gx=1&.rand=fegr2vucbecu5  https://www.mybank.com:8080/abc/xyz  www.mybank.com  ftp://www.mycharity.org/abc/xyz  extract_hostname.sql
  • 24. Convert value pairs to XML  Input: A string such as 'remain1=1;remain2=2;'  Output: An XML string <remain1><value=1></remain1> <remain2><value=2></remain2>  convert_to_xml.sql
  • 25. Sort IP addresses in numerical order  Sort IP addresses, that are stored as character strings, in numerical order.  Input  10.10.20.10  127.0.0.1  166.22.33.44  187.222.121.0  20.23.23.20  sort_ip_address.sql
  • 26. Extract first name, last name, and middle initial  Extract the first name, last name with an optional middle initial.  first_last_mi.sql
  • 27. Finding the Last Occurrence  Find the last numeric sequence from a sequence.  Return 567 from 'abc/123/def567/xyz'  INSTR and SUBSTR allow backward search when position is negative. REGEXP functions don't allow backward search  last_occurrence.sql
  • 28. Fuzzy Match  Tables t1 and t2 each have a varchar2(12) column (t1.x, t2.y).  A row in t1 is considered a match for a row in t2, if any six characters in t1.x matches with any six characters in t2.y  fuzzy_match.sql
  • 29. The lazy operator  ? is lazy/non-greedy quantifier  greedy_lazy.sql
  • 30. Meta-characters with multiple meanings  Same meta characters are used with multiple meanings  ^ used for anchoring and negation.  ? used as quantifier and lazy operator  () used for grouping or sub-expression  metachars_with_multiple_meanings.sql
  • 31. Nuances  ? (zero or one), * (zero or more) could sometimes mislead you  nuances.sql
  • 32. Stored patterns  patterns can be stored in table columns and be referenced in REGEXP functions  No need to hard-code them  stored_patterns.sql
  • 33. Random things  Insert a dash before the two last digits  Remove a substring  Get rid of useless commas from a string  Find the word that comes immediately before a substring (e.g. XXX)  Replace whole words, not its parts  Trimming the trailing digits  random.sql
  • 34. A few other points  When not to use Regular Expressions  If the same thing could be used without regular expressions and without too much coding.  POSIX notations need double brackets [[:upper]]. [:upper:] won't work. [[:UPPER:]] won't work either. It has to be in lower case letters.  Locale support provided with Collation Element ][.ce.]], and Equivalence Classes [[=e=]]  MySQL supports regular expressions with RLIKE
  • 35. References  Oracle® Database Advanced Application Developer's Guide (http://download.oracle.com/docs/cd/E11882_0 1/appdev.112/e17125/adfns_regexp.htm#CHDGH BHF)  Anti-Patterns in Regular Expressions: http://gennick.com/antiregex.html  First Expressions. An article by Jonathan Gennick Oracle Magazine, Sep/Oct 2003.  Oracle Regular Expressions Pocket Reference by Gonathan Gennick  http://examples.oreilly.com/9780596006013/Re gexPocketData.sql
  • 36. References ...  http://www.psoug.org/reference/regexp.html  http://download.oracle.com/docs/cd/E11882_01/se rver.112/e10592/conditions007.htm#SQLRF00501  http://www.oracle.com/technology/pub/articles/sat ernos_regexp.html  http://www.oracle.com/technology/products/datab ase/application_development/pdf/TWP_Regular_E xpressions.pdf  http://asktom.oracle.com/pls/asktom/asktom.searc h?p_string=regexp_
  • 37. References ...  http://www.oracle.com/technology/obe/10gr2_db_single/de velop/regexp/regexp_otn.htm  http://www.oracle.com/technology/sample_code/tech/pl_sq l/index.html  http://forums.oracle.com/forums/thread.jspa?threadID=427 716  http://forums.oracle.com/forums/search.jspa?threadID=&q= regular+expression&objID=f75&dateRange=all&userID=&nu mResults=120&rankBy=9  http://www.oracle.com/technology/sample_code/tech/pl_sq l/index.html  http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUE STION_ID:2200894550208#1568589800346862515