<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RegEx Archives - Philipp Salvisberg&#039;s Blog</title>
	<atom:link href="https://www.salvis.com/blog/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.salvis.com/blog/tag/regex/</link>
	<description>Database-centric development</description>
	<lastBuildDate>Wed, 08 Nov 2023 04:11:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.salvis.com/blog/wp-content/uploads/2014/04/favicon.png</url>
	<title>RegEx Archives - Philipp Salvisberg&#039;s Blog</title>
	<link>https://www.salvis.com/blog/tag/regex/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>
<atom:link rel="hub" href="https://websubhub.com/hub"/>
<atom:link rel="self" href="https://www.salvis.com/blog/tag/regex/feed/"/>
	<item>
		<title>Regular Expressions in SQL by Examples</title>
		<link>https://www.salvis.com/blog/2018/09/28/regular-expressions-sql-examples/</link>
		
		<dc:creator><![CDATA[Philipp Salvisberg]]></dc:creator>
		<pubDate>Fri, 28 Sep 2018 14:28:36 +0000</pubDate>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://www.salvis.com/blog/?p=8917</guid>

					<description><![CDATA[<p>Are you reluctant to use regular expressions in SQL? Then continue reading. Examples helped me to understand regular expressions years ago. Thus I hope this collection of simple examples and the tooling tips will encourage you to use regular expressions. It&#8217;s not as complicated as it looks at first glance. Once you<span class="excerpt-hellip"> […]</span></p>
<p>The post <a href="https://www.salvis.com/blog/2018/09/28/regular-expressions-sql-examples/">Regular Expressions in SQL by Examples</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Are you reluctant to use regular expressions in SQL? Then continue reading. Examples helped me to understand regular expressions years ago. Thus I hope this collection of simple examples and the tooling tips will encourage you to use regular expressions. It&#8217;s not as complicated as it looks at first glance. Once you get used to the syntax, it&#8217;s fun to figure out the right match pattern.</p>



<h2 class="wp-block-heading">Use Cases in SQL</h2>



<p>The Oracle Database supports regular expression since version 10g Release 1. You may use it to:</p>



<ol class="wp-block-list">
<li>Validate an input using <code><a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/Pattern-matching-Conditions.html#GUID-D2124F3A-C6E4-4CCA-A40E-2FFCABFD8E19">regexp_like</a></code>;</li>



<li>Find patterns in text using <code><a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/REGEXP_COUNT.html#GUID-5148AF2E-9CED-497D-A78D-3A7847A45276">regexp_count</a></code>, <code><a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/REGEXP_INSTR.html#GUID-D21B53A1-83E2-4722-9BBB-638470715DD6">regexp_instr</a></code> and <code><a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/REGEXP_SUBSTR.html#GUID-2903904D-455F-4839-A8B2-1731EF4BD099">regexp_substr</a></code>;</li>



<li>Find and replace patterns in text using <code><a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/REGEXP_REPLACE.html#GUID-EA80A33C-441A-4692-A959-273B5A224490">regexp_replace</a></code>.</li>
</ol>



<p>Finding text using regular expressions is known as pattern matching. Those who understand regular expressions will quickly find their way around <a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/dwhsg/sql-pattern-matching-data-warehouses.html#GUID-136DAC89-DA17-45C6-9E37-C9892723AC79">row pattern matching</a>, since the pattern syntax is very similar.</p>



<h2 class="wp-block-heading">The Text</h2>



<p>All examples use this famous quote from Henry Ford:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right."<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p>All matches are <span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">highlighted</span>
</span>.</p>



<h2 class="wp-block-heading">Single Character</h2>



<p>The simplest match pattern (regular expression without match parameters) is a single character. There are some characters with a special meaning such as <code>.</code>, <code>\</code>, <code>?</code>, <code>*</code>, <code>+</code>, <code>{</code>, <code>}</code>, <code>[</code>, <code>]</code>, <code>^</code>, <code>$</code>, <code>|</code>, <code>(</code>, <code>)</code>. We deal with these characters later. However, as long as you do not use one of these characters, the match pattern behaves like the <code>substring</code> parameter in the well-known <code><a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/INSTR.html#GUID-47E3A7C4-ED72-458D-A1FA-25A9AD3BE113">instr</a></code> function.</p>



<p>Match pattern: <code>t</code> returns 5 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whe<span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">t</span>
</span>her you <span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">t</span>
</span>hink you can or <span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">t</span>
</span>hink you can'<span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">t</span>
</span> - you are righ<span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">t</span>
</span>."<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p>The following query produces a row per match. It can be used in the subsequent examples as well (with an adapted match pattern).</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">1) One row per match in SQL</span><span role="button" tabindex="0" data-code="WITH 
   base AS (
      SELECT '&quot;Whether you think you can or think you can''t - you are right.&quot;' 
             || chr(10) || '-- Henry Ford (1863 - 1947)' AS text,
             't' AS pattern
        FROM dual
   )
-- main
 SELECT regexp_substr(text, pattern, 1, level) AS matched_text,
        regexp_instr(text, pattern, 1, level) AS at_pos
   FROM base
CONNECT BY level <= regexp_count(text, pattern);

MATCHED_TEXT             AT_POS
-------------------- ----------
t                             5
t                            14
t                            31
t                            45
t                            61" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">WITH</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">   base </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> (</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">SELECT</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;&quot;Whether you think you can or think you can&#39;&#39;t - you are right.&quot;&#39;</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">             || chr(</span><span style="color: #B5CEA8">10</span><span style="color: #D4D4D4">) || </span><span style="color: #CE9178">&#39;-- Henry Ford (1863 - 1947)&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">             </span><span style="color: #CE9178">&#39;t&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> pattern</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">FROM</span><span style="color: #D4D4D4"> dual</span></span>
<span class="line"><span style="color: #D4D4D4">   )</span></span>
<span class="line"><span style="color: #6A9955">-- main</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4"> </span><span style="color: #569CD6">SELECT</span><span style="color: #D4D4D4"> regexp_substr(</span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">, pattern, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> matched_text,</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">        regexp_instr(</span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">, pattern, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> at_pos</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">FROM</span><span style="color: #D4D4D4"> base</span></span>
<span class="line cbp-line-highlight"><span style="color: #569CD6">CONNECT</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">BY</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4"> &lt;= regexp_count(</span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">, pattern);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">MATCHED_TEXT             AT_POS</span></span>
<span class="line"><span style="color: #6A9955">-------------------- ----------</span></span>
<span class="line"><span style="color: #D4D4D4">t                             </span><span style="color: #B5CEA8">5</span></span>
<span class="line"><span style="color: #D4D4D4">t                            </span><span style="color: #B5CEA8">14</span></span>
<span class="line"><span style="color: #D4D4D4">t                            </span><span style="color: #B5CEA8">31</span></span>
<span class="line"><span style="color: #D4D4D4">t                            </span><span style="color: #B5CEA8">45</span></span>
<span class="line"><span style="color: #D4D4D4">t                            </span><span style="color: #B5CEA8">61</span></span></code></pre></div>



<p>The named subquery <code>base</code> provides the text and the match pattern. This way the expressions do not have to be repeated. The <code>regexp_count</code> function on line 12 limits the result to 5 rows. The <code>regexp_substr</code> function call on line 9 returns the matched text and the <code>regexp_instr</code> function call on line 10 the position.</p>



<h2 class="wp-block-heading">Multiple Characters</h2>



<p>A string is just a series of characters.</p>



<p>The match pattern <code>thin</code> returns 2 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you <span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">thin</span>
</span>k you can or <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">thin</span>
k you can't - you are right."<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<h2 class="wp-block-heading">Any Character Wildcard <code>.</code></h2>



<p>A dot <code>.</code> matches per default any character except newline <code>chr(10)</code>.</p>



<p>The match pattern <code>c.n</code> returns 2 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
 or think you <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
't - you are right."<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Escape Character <code>\</code></h2>



<p>If we want to match special characters such as a dot <code>.</code> than we have to escape it with a <code>\</code>.</p>



<p>The match pattern <code>\.</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">.</span>
"<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<h2 class="wp-block-heading">0..1 Matches (Optionality) <code>?</code></h2>



<p>We use a <code>?</code> to express that a character (or a group of characters) is optional.</p>



<p>The match pattern <code>c?.n</code> returns 5 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you th<span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">in</span>
</span>k you <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
 or th<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">in</span>
k you <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
't - you are right."<br />-- H<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">en</span>
ry Ford (1863 - 1947)</p>
</blockquote>



<p>You see that the <code>c</code> is part of a match in <code>can</code>, but <code>h</code> before <code>in</code> is not.</p>



<h2 class="wp-block-heading">0..n Matches <code>*</code></h2>



<p>We use a <code>*</code> to express that a character (or a group of characters) can appear between <code>0</code> and <code>n</code> times. <code>n</code> is not defined and is in fact unbounded.</p>



<p>The match pattern <code>you.*n</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether <span style="color: #aa1100;"><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you think you can or think you can</span>
</span>'t&nbsp;- you are right."<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p>Please note that the first match was not <code>you thin</code>. Rather it was extended to the last <code>n</code> in the first line. This behaviour is called <strong>greedy</strong>.</p>



<h2 class="wp-block-heading">Nongreedy Matches <code>?</code></h2>



<p>We use a <code>?</code> at the end of the quantifier (<code>?</code>, <code>*</code>, <code>+</code>, <code>{}</code>) to match as few as possible.</p>



<p>The match pattern <code>you.*?n</code> returns 3 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you thin</span>
k <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you can</span>
 or think <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you can</span>
't - you are right."<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p>Please note that we now have three matches. This behavior is called <strong>nongreedy</strong> or <strong>reluctant</strong> or <strong>lazy</strong>.</p>



<h2 class="wp-block-heading">1..n Matches <code>+</code></h2>



<p>We use a <code>+</code> to express that a character (or a group of characters) can appear between <code>1</code> and <code>n</code> times. <code>n</code> is not defined and is in fact unbounded.</p>



<p>The match pattern <code>-+</code> returns 3 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">-</span>
 you are right."<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">--</span>
 Henry Ford (1863 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">-</span>
 1947)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Exact Match <code>{n}</code></h2>



<p>We use <code>{n}</code> to express that a character (or a group of characters) must appear exactly <code>n</code> times.</p>



<p>The match pattern <code>-{2}</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right."<br /><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">--</span>
 Henry Ford (1863 - 1947)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Match Ranges <code>{m,n}</code></h2>



<p>We use <code>{m,n}</code> to express that a character (or a group of characters) must appear between <code>m</code> and <code>n</code> times. You may skip the definition for <code>n</code> to express an unbounded value.</p>



<p>The match pattern <code>-{1,3}</code> returns 3 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">-</span>
 you are right.&#8221;<br /><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">--</span>
 Henry Ford (1863 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">-</span>
 1947)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Alphanumeric Wildcard <code>\w</code></h2>



<p>A <code>\w</code> matches any alphanumeric character.</p>



<p>The match pattern <code>\w+</code> returns 17 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Whether</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">or</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
'<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">t</span>
 - <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">are</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">right</span>
."<br />-- <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Henry</span>
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Ford</span>
 (<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">1863</span>
 - <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">1947</span>
)</p>
</blockquote>



<p></p>



<p></p>



<h2 class="wp-block-heading">Non-alphanumeric Wildcard <code>\W</code></h2>



<p>A <code>\W</code> matches any non-alphanumeric character. Please note that the match pattern is case-sensitive. The upper case letter <code>W</code> leads to the opposite result than the lower case letter <code>w</code>. This is an essential principle for match patterns.</p>



<p>The match pattern <code>\W+</code> returns 18 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">"</span>
Whether<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
think<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
can<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
or<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
think<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
can<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">'</span>
t<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> - </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
are<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
right<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">."
<br />-- </span>
Henry<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
Ford<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> (</span>
1863 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> - </span>
1947<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">)</span>
</p>



<p></p>



<p>It&#8217;s important to note that the newline chr(10) is part of match 14.</p>
</blockquote>



<h2 class="wp-block-heading">Digit Wildcard <code>\d</code></h2>



<p>A <code>\d</code> matches any digit (<code>0</code> to <code>9</code>).</p>



<p>The match pattern <code>\d+</code> returns 2 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right."<br />-- Henry Ford (<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">1863</span>
 - <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">1947</span>
)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Non-digit Wildcard <code>\D</code></h2>



<p>A <code>\D</code> matches any non-digit character. Please note that the match pattern is case-sensitive. The upper case letter <code>D</code> leads to the opposite result than the lower case letter <code>d</code>. This is an essential principle for match patterns.</p>



<p>The match pattern <code>\D+</code> returns 3 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">"Whether you think you can or think you can't - you are right."<br />-- Henry Ford (</span>
1863<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> - </span>
1947<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">)</span>
</p>
</blockquote>



<p>It&#8217;s important to note that the newline <code>chr(10)</code> is part of the first match.</p>



<h2 class="wp-block-heading">Whitespace Wildcard <code>\s</code></h2>



<p>A <code>\s</code> matches any whitespace character. Whitespace are:</p>



<ul class="wp-block-list">
<li>spaces <code>chr(32)</code></li>



<li>horizontal tabs <code>chr(9)</code></li>



<li>carriage returns <code>chr(13)</code></li>



<li>line feeds/newlines <code>chr(10)</code></li>



<li>form feeds <code>chr(12)</code></li>



<li>vertical tabs <code>chr(11)</code></li>
</ul>



<p>The match pattern <code>\s+</code> returns 18 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
think<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
can<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
or<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
think<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
can't<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
-<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
are<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
right."<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">
</span>
<br />--<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
Henry<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
Ford<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
(1863<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
-<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
1947)</p>
</blockquote>



<p></p>



<p>It&#8217;s important to note that the match 13 is a newline <code>chr(10)</code>.</p>



<h2 class="wp-block-heading">Non-whitespace Wildcard <code>\S</code></h2>



<p>A <code>\S</code>&nbsp;matches any non-whitespace character. Please note that the match pattern is case-sensitive. The upper case letter <code>S</code> leads to the opposite result than the lower case letter <code>s</code>. This is an essential principle for match patterns.</p>



<p>The match pattern <code>\S+</code> returns 19 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">"Whether</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">or</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can't</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">-</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">are</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">right."</span>
<br /><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">--</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Henry</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Ford</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">(1863</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">-</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">1947)</span>
</p>
</blockquote>



<h2 class="wp-block-heading">Character Class <code>[xyz]</code></h2>



<p>A character class is a list of characters defined within brackets. You can also use a hyphen <code>-</code> to specify a range of characters. For example <code>[0-9]</code> which is equivalent to <code>\d</code>. You can combine ranges and single characters.</p>



<p>The match pattern <code>[a-zA-Z']+</code> returns 14 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Whether</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">or</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can't</span>
 - <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">you</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">are</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">right</span>
."<br />-- <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Henry</span>
 <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Ford</span>
 (1863 - 1947)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Negated Character Class <code>[^xyz]</code></h2>



<p>A negated character class matches all characters that are not defined within brackets. A <code>^</code> at the first position within the brackets defines a negated character class.</p>



<p>The match pattern <code>[^a-zA-Z']+</code> returns 15 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">"</span>
Whether<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
think<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
can<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
or<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
think<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
can't<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> - </span>
you<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
are<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
right<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">."
<br />-- </span>
Henry<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> </span>
Ford<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;"> (1863 - 1947)</span>
</p>
</blockquote>



<p>It&#8217;s important to note that the newline <code>chr(10)</code> is part of match 13.</p>



<h2 class="wp-block-heading">Beginning of Line or String <code>^</code></h2>



<p>A <code>^</code> matches the position before the first character within a line or string. By default a text is treated as a string.</p>



<p>The match pattern <code>^-</code> returns 0 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right."<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p>It&#8217;s important to note that by default the whole text is treated as a single line. Hence <code>^</code> means beginning of string. And the string starts with a <code>"</code> and not with a <code>-</code>. Therefore no matches.</p>



<h2 class="wp-block-heading">Multiline Mode <code>m</code></h2>



<p>A regular expressions has two parts. The first part is the match pattern. The second part are match parameters. Until now we have not defined match parameters, hence the default has been used. The match parameter <code>m</code> will logically change the text from a single line to an array of lines.</p>



<p>The match pattern <code>^-</code> with the match parameter <code>m</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right."<br /><span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">-</span>
- Henry Ford (1863 - 1947)</p>
</blockquote>



<p>The next query produces a row per match as the query above, but applies the match parameter <code>m</code>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">2) One row per match in SQL with match parameter</span><span role="button" tabindex="0" data-code="WITH 
   base AS (
      SELECT '&quot;Whether you think you can or think you can''t - you are right.&quot;' 
             || chr(10) || '-- Henry Ford (1863 - 1947)' AS text,
             '^-' AS pattern,
             'm' AS param
        FROM dual
   )
-- main
 SELECT regexp_substr(text, pattern, 1, level, param) AS matched_text,
        regexp_instr(text, pattern, 1, level, 0, param) AS at_pos
   FROM base
CONNECT BY level <= regexp_count(text, pattern, 1, param);

MATCHED_TEXT             AT_POS
-------------------- ----------
-                            65" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">WITH</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">   base </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> (</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">SELECT</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;&quot;Whether you think you can or think you can&#39;&#39;t - you are right.&quot;&#39;</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">             || chr(</span><span style="color: #B5CEA8">10</span><span style="color: #D4D4D4">) || </span><span style="color: #CE9178">&#39;-- Henry Ford (1863 - 1947)&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">             </span><span style="color: #CE9178">&#39;^-&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> pattern,</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">             </span><span style="color: #CE9178">&#39;m&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">param</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">FROM</span><span style="color: #D4D4D4"> dual</span></span>
<span class="line"><span style="color: #D4D4D4">   )</span></span>
<span class="line"><span style="color: #6A9955">-- main</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4"> </span><span style="color: #569CD6">SELECT</span><span style="color: #D4D4D4"> regexp_substr(</span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">, pattern, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">param</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> matched_text,</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">        regexp_instr(</span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">, pattern, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">param</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> at_pos</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">FROM</span><span style="color: #D4D4D4"> base</span></span>
<span class="line"><span style="color: #569CD6">CONNECT</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">BY</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4"> &lt;= regexp_count(</span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">, pattern, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">param</span><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">MATCHED_TEXT             AT_POS</span></span>
<span class="line"><span style="color: #6A9955">-------------------- ----------</span></span>
<span class="line"><span style="color: #D4D4D4">-                            </span><span style="color: #B5CEA8">65</span></span></code></pre></div>



<p>The match parameter is defined on line 6. The <code>regex_substr</code> function call on line 10 and the <code>regex_instr</code> function call on line 11 get this match parameter as an additional input.</p>



<p>You may use this query with adapted match pattern and match parameters to reproduce the results of the subsequent examples.</p>



<h2 class="wp-block-heading">End of Line or String <code>$</code></h2>



<p>A <code>$</code> matches the position after the last character within a line or string. By default a text is treated as a string.</p>



<p>The match pattern <code>"$</code> with the match parameter <code>m</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right.<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">"</span>
<br />-- Henry Ford (1863 - 1947)</p>
</blockquote>





<p></p>



<h2 class="wp-block-heading">Ignore Case Mode <code>i</code></h2>



<p>Use the match parameter <code>i</code> for case-insensitive matches.</p>



<p>The match pattern <code>he</code> with the match parameter <code>i</code> returns 3 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"W<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">he</span>
t<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">he</span>
r you think you can or think you can't - you are right."
<br />-- <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">He</span>
nry Ford (1863 - 1947)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Case-sensitive Mode <code>c</code></h2>



<p>Use the match parameter <code>c</code> for case-sensitive matches. This is the default. However, when <code>NLS_SORT</code> is set to a case-insensitive sort order &#8211; e.g. <code>BINARY_CI</code>, <code>GENERIC_M_CI</code>, <code>FRENCH_M_CI</code>, etc. &#8211; then the default changes to case-insensitive matches.</p>



<p>The match pattern <code>he</code> with the match parameter <code>c</code> returns 2 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"W<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">he</span>
t<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">he</span>
r you think you can or think you can't - you are right."<br />
-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p></p>



<h2 class="wp-block-heading">Period Matches Newline Mode <code>n</code></h2>



<p>Use the match parameter <code>n</code> to change the behavior of the any character wildcard <code>.</code> to match newlines <code>chr(10)</code> as well.</p>



<p>The match pattern <code>.+</code> with the match parameter <code>n</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">"Whether you think you can or think you can't - you are right."
<br />-- Henry Ford (1863 - 1947)</span>

</blockquote>





<h2 class="wp-block-heading">Ignore Whitespace in Pattern Mode <code>x</code></h2>



<p>Use the match parameter&nbsp;<code>x</code> to ignore whitespace in match patterns. For long match patterns it might be helpful to add spaces, tabs and newlines to make the regular expressions more readable. By default these whitespace are considered to be part of the match pattern. To ignore them you have to use the <code>x</code> mode. However, whitespace in brackets are always considered, e.g. <code>[ ]</code>.</p>



<p>The match pattern <code>h&nbsp; e nr y</code> with the match parameters <code>ix</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you think you can or think you can't - you are right."<br />-- <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">Henry</span>
 Ford (1863 - 1947)</p>
</blockquote>



<p></p>



<p>Please note that multiple match parameters (<code>i</code> and <code>x</code>) are used.</p>



<h2 class="wp-block-heading">Alternatives <code>|</code></h2>



<p>Use a <code>|</code> to express alternative options. The number of options is not limited. The order of the options corresponds to the priority.</p>



<p>The match pattern&nbsp; <code>think|can't|can</code> returns 4 matches:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>"Whether you <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
 you <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can</span>
 or <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">think</span>
 you <span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">can't</span>
 - you are right."
-- Henry Ford (1863 - 1947)</p>
</blockquote>



<p></p>



<p>It&#8217;s important to note that the order of the options matter in this case. The match pattern <code>think|can|can't</code> would never match <code>can't</code>. Furthermore, to avoid redundancies in match patterns you would use groups. For example <code>think|can('t)?</code>.</p>



<h2 class="wp-block-heading">Numbered Groups <code>(xyz)</code></h2>



<p>Use parenthesis &#8211; <code>(</code> and <code>)</code> &#8211; to define groups. You may nest groups as well. The complete match pattern is group 0. All other (sub-)groups are numbered from left to right. You may simply count the number of open parenthesis in a match pattern up to the cursor position of a group to determine the group number.</p>



<p>The match pattern&nbsp;<code>^</code><code>("|')</code><code>(.+)</code><code>(\1)</code><code>\s+--\s+</code><code>(\w+)</code><code>\s+</code><code>(\w+)</code><code>\s+</code><code>(\(</code><code>(\d+)</code><code>\s*-\s*</code><code>(\d+)</code><code>\))</code><code>$</code> returns 1 match:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<span class="highlight highlight-" style="background-color:Lemonchiffon;color:black;">"Whether you think you can or think you can't - you are right."<br />
-- Henry Ford (1863 - 1947)</span>

</blockquote>



<p>The matches for the groups are:</p>



<ul class="wp-block-list">
<li>0=full match (as shown above)</li>



<li>1=<code>"</code></li>



<li>2=<code>Whether … right.</code></li>



<li>3=<code>"</code></li>



<li>4=<code>Henry</code></li>



<li>5=<code>Ford</code></li>



<li>6=<code>(1863 - 1947)</code></li>



<li>7=<code>1863</code></li>



<li>8=<code>1947</code></li>
</ul>



<p>Please note that the group 3 in the match pattern is referencing the result of the group 1 (<code>"</code>). This means a quote starting with <code>'</code> must end on <code>'</code> and a quote starting with <code>"</code> must end on <code>"</code>.</p>



<p>The next query produces a row per group.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">3) One row per group in SQL</span><span role="button" tabindex="0" data-code="WITH 
   base AS (
      SELECT '&quot;Whether you think you can or think you can''t - you are right.&quot;' 
             || chr(10) || '-- Henry Ford (1863 - 1947)' AS text,
             '^(&quot;|'')(.+)(\1)\s+--\s+(\w+)\s+(\w+)\s+(\((\d+)\s*-\s*(\d+)\))$' AS pattern
        FROM dual
   )
-- main
 SELECT level-1 AS group_no,
        regexp_substr(text, pattern, 1, 1, null, level-1) AS matched_group_text
   FROM base
CONNECT BY level &lt;= regexp_count(pattern, '[^\\]?\(') + 1;
 
GROUP_NO MATCHED_GROUP_TEXT                                              
-------- ----------------------------------------------------------------
       0 &quot;Whether you think you can or think you can't - you are right.&quot; 
         -- Henry Ford (1863 - 1947)                                     
 
       1 &quot;                                                               
       2 Whether you think you can or think you can't - you are right.   
       3 &quot;                                                               
       4 Henry                                                           
       5 Ford                                                            
       6 (1863 - 1947)                                                   
       7 1863                                                            
       8 1947                                                            
 
9 rows selected." style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">WITH</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">   base </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> (</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">SELECT</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;&quot;Whether you think you can or think you can&#39;&#39;t - you are right.&quot;&#39;</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">             || chr(</span><span style="color: #B5CEA8">10</span><span style="color: #D4D4D4">) || </span><span style="color: #CE9178">&#39;-- Henry Ford (1863 - 1947)&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">             </span><span style="color: #CE9178">&#39;^(&quot;|&#39;&#39;)(.+)(\1)\s+--\s+(\w+)\s+(\w+)\s+(\((\d+)\s*-\s*(\d+)\))$&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> pattern</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #569CD6">FROM</span><span style="color: #D4D4D4"> dual</span></span>
<span class="line"><span style="color: #D4D4D4">   )</span></span>
<span class="line"><span style="color: #6A9955">-- main</span></span>
<span class="line"><span style="color: #D4D4D4"> </span><span style="color: #569CD6">SELECT</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4">-</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> group_no,</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">        regexp_substr(</span><span style="color: #569CD6">text</span><span style="color: #D4D4D4">, pattern, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">null</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4">-</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">AS</span><span style="color: #D4D4D4"> matched_group_text</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">FROM</span><span style="color: #D4D4D4"> base</span></span>
<span class="line"><span style="color: #569CD6">CONNECT</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">BY</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4"> &lt;= regexp_count(pattern, </span><span style="color: #CE9178">&#39;[^\\]?\(&#39;</span><span style="color: #D4D4D4">) + </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">GROUP_NO MATCHED_GROUP_TEXT                                              </span></span>
<span class="line"><span style="color: #6A9955">-------- ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #D4D4D4">       </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;Whether you think you can or think you can&#39;t - you are right.&quot;</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #6A9955">-- Henry Ford (1863 - 1947)                                     </span></span>
<span class="line"><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">       </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;                                                               </span></span>
<span class="line"><span style="color: #CE9178">       2 Whether you think you can or think you can&#39;t - you are right.   </span></span>
<span class="line"><span style="color: #CE9178">       3 &quot;</span><span style="color: #D4D4D4">                                                               </span></span>
<span class="line"><span style="color: #D4D4D4">       </span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> Henry                                                           </span></span>
<span class="line"><span style="color: #D4D4D4">       </span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4"> Ford                                                            </span></span>
<span class="line"><span style="color: #D4D4D4">       </span><span style="color: #B5CEA8">6</span><span style="color: #D4D4D4"> (</span><span style="color: #B5CEA8">1863</span><span style="color: #D4D4D4"> - </span><span style="color: #B5CEA8">1947</span><span style="color: #D4D4D4">)                                                   </span></span>
<span class="line"><span style="color: #D4D4D4">       </span><span style="color: #B5CEA8">7</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1863</span><span style="color: #D4D4D4">                                                            </span></span>
<span class="line"><span style="color: #D4D4D4">       </span><span style="color: #B5CEA8">8</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1947</span><span style="color: #D4D4D4">                                                            </span></span>
<span class="line"><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #B5CEA8">9</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">rows</span><span style="color: #D4D4D4"> selected.</span></span></code></pre></div>



<p>The <code>regex_substr</code> function call on line 10 gets the group number as the last input parameter.</p>



<h2 class="wp-block-heading">Tooling</h2>



<p>The match pattern used in the previous example is not that easy to read. Hence I recommend using some tools to build regular expressions. These tools provide quick references and libraries for common regular expressions. And of course, they provide features to test regular expressions and show matches. But they also can explain a regular expression in detail. Here are three of them:</p>



<ul class="wp-block-list">
<li><a href="http://www.ultrapico.com/expresso.htm">Expresso</a> is a longtime, reliable companion of mine. This tool has helped me to build and understand many regular expressions. It runs under Windows and is free, but requires registration.</li>



<li><a href="https://regex101.com/">regular expressions 101</a> is a popular online regular expressions tester and debugger.</li>



<li><a href="https://regexr.com/">RegExr</a> is another popular online tool to learn, test and build regular expressions.</li>
</ul>



<p>Here&#8217;s a screenshot of Expresso showing the match results and some explanation of the regular expression.</p>



<figure class="wp-block-image"><a href="https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3.png"><img fetchpriority="high" decoding="async" width="1405" height="1009" src="https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3.png" alt="" class="wp-image-9049" srcset="https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3.png 1405w, https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3-300x215.png 300w, https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3-768x552.png 768w, https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3-1024x735.png 1024w, https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3-203x146.png 203w, https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3-50x36.png 50w, https://www.salvis.com/blog/wp-content/uploads/2018/09/expresso3-104x75.png 104w" sizes="(max-width:767px) 480px, (max-width:1405px) 100vw, 1405px" /></a></figure>



<p>It&#8217;s important to note that the <a href="https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/Oracle-Regular-Expression-Support.html#GUID-969230D6-FC1A-4C75-BF2A-6B1BE909DED6">regular expressions in the Oracle Database</a> conforms to <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html">POSIX</a> with a few extensions influenced by <a href="http://www.pcre.org/current/doc/html/pcre2syntax.html">PCRE</a>. So these tools support regular expression features which are not available in Oracle SQL. I miss for example non-capturing groups, lookaheads and some escaped characters (<code>\r</code>, <code>\n</code>, <code>\t</code>, etc.).</p>



<h2 class="wp-block-heading">Summary</h2>



<p>Regular expressions are not self-explanatory. In this post, I covered most of the regular expression grammar that is applicable in SQL functions of an Oracle Database.</p>



<ul class="wp-block-list">
<li>Strings: <code>t</code>, <code>thin</code></li>



<li>Greedy quantifiers: <code>?</code>, <code>*</code>, <code>+</code>, <code>{2}</code>, <code>{1, 3}</code></li>



<li>Nongreedy quantifiers: <code>??</code>, <code>*?</code>, <code>+?</code>, <code>{2}?</code>, <code>{1, 3}?</code></li>



<li>Character classes: <code>.</code>, <code>\.</code>, <code>\w</code>, <code>\W</code>, <code>\d</code>, <code>\D</code>, <code>\s</code>, <code>\S</code>, <code>[a-z]</code>, <code>[^a-z]</code></li>



<li>Positions: <code>^</code>, <code>$</code></li>



<li>Alternatives: <code>|</code></li>



<li>Numbered groups: <code>(abc)</code>, <code>\1</code>, <code>\2</code>, &#8230;, <code>\9</code></li>



<li>Match parameters: <code>m</code>, <code>i</code>, <code>c</code>, <code>n</code>, <code>x</code></li>
</ul>



<p>With a basic knowledge of regular expressions, the available tooling makes building, testing and understanding regular expressions quite easy.</p>
<p>The post <a href="https://www.salvis.com/blog/2018/09/28/regular-expressions-sql-examples/">Regular Expressions in SQL by Examples</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
