<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <title>Replacing and Splitting
</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="Composable F# regular expressions."/>
    <meta name="author" content="Jack Fox"/>

    <script src="https://code.jquery.com/jquery-1.8.0.js"></script>
    <script src="https://code.jquery.com/ui/1.8.23/jquery-ui.js"></script>
    <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>
    <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.1/css/bootstrap-combined.min.css" rel="stylesheet"/>

    <link type="text/css" rel="stylesheet" href="/FsRegEx/content/style.css" />
    <script type="text/javascript" src="/FsRegEx/content/tips.js"></script>
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">
      <div class="masthead">
        <ul class="nav nav-pills pull-right">
          <li><a href="http://fsharp.org">fsharp.org</a></li>
          <li><a href="https://github.com/jackfoxy/FsRegEx">github page</a></li>
        </ul>
        <h3 class="muted"><a href="/FsRegEx/index.html">FsRegEx</a></h3>
      </div>
      <hr />
      <div class="row">
        <div class="span9" id="main">
          
<h1><a name="Replacing-and-Splitting" class="anchor" href="#Replacing-and-Splitting">Replacing and Splitting</a></h1>
<h2><a name="Replace" class="anchor" href="#Replace">Replace</a></h2>
<p>The <code>System.Text.RegularExpressions.Regex</code> Class provides several overloads of <code>replace</code>. The same overloads are available in the <code>FsRegEx</code> class and as individually
named functions, including overloads and functions using <code>RegexOptions</code>. <code>Timeout</code> is not supported at this time.</p>
<h3><a name="simple-replacement-of-all-occurence" class="anchor" href="#simple-replacement-of-all-occurence">simple replacement of all occurence</a></h3>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
<span class="l">4: </span>
<span class="l">5: </span>
<span class="l">6: </span>
<span class="l">7: </span>
<span class="l">8: </span>
<span class="l">9: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="k">open</span> <span onmouseout="hideTip(event, 'fs1', 1)" onmouseover="showTip(event, 'fs1', 1)" class="id">System</span>
<span class="k">open</span> <span onmouseout="hideTip(event, 'fs1', 2)" onmouseover="showTip(event, 'fs1', 2)" class="id">System</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs2', 3)" onmouseover="showTip(event, 'fs2', 3)" class="id">Text</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs3', 4)" onmouseover="showTip(event, 'fs3', 4)" class="id">RegularExpressions</span>

<span class="s">&quot;This is   text with   far  too   much   &quot;</span> <span class="o">+</span> 
                <span class="s">&quot;whitespace.&quot;</span>
<span class="o">|&gt;</span> <span class="id">FsRegEx</span><span class="pn">.</span><span class="id">replace</span> <span class="s">&quot;\\s+&quot;</span> <span class="s">&quot; &quot;</span>
<span class="o">|&gt;</span><span onmouseout="hideTip(event, 'fs4', 5)" onmouseover="showTip(event, 'fs4', 5)" class="fn">printfn</span> <span class="s">&quot;Replacement String: </span><span class="pf">%s</span><span class="s">&quot;</span> 

<span class="c">// Replacement String: This is text with far too much whitespace.</span>
</code></pre></td>
</tr>
</table>
<h3><a name="replace-max-time-starting-at" class="anchor" href="#replace-max-time-starting-at">replace max time starting at</a></h3>
<p>Replaces a specified maximum number of strings starting at location that match a regular expression pattern with a specified replacement string.</p>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l"> 1: </span>
<span class="l"> 2: </span>
<span class="l"> 3: </span>
<span class="l"> 4: </span>
<span class="l"> 5: </span>
<span class="l"> 6: </span>
<span class="l"> 7: </span>
<span class="l"> 8: </span>
<span class="l"> 9: </span>
<span class="l">10: </span>
<span class="l">11: </span>
<span class="l">12: </span>
<span class="l">13: </span>
<span class="l">14: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="s">&quot;Instantiating a New Type\n&quot;</span> <span class="o">+</span>
    <span class="s">&quot;Generally, there are two ways that an\n&quot;</span> <span class="o">+</span> 
    <span class="s">&quot;instance of a class or structure can\n&quot;</span> <span class="o">+</span>
    <span class="s">&quot;be instantiated. &quot;</span>
<span class="o">|&gt;</span> <span class="id">FsRegEx</span><span class="pn">.</span><span class="id">replaceMaxTimesStartAtOpt</span> <span class="s">&quot;^.*$&quot;</span> <span onmouseout="hideTip(event, 'fs5', 6)" onmouseover="showTip(event, 'fs5', 6)" class="id">RegexOptions</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs6', 7)" onmouseover="showTip(event, 'fs6', 7)" class="id">Multiline</span> <span class="s">&quot;\n$&amp;&quot;</span>  <span class="o">-</span><span class="n">1</span> <span class="n">1</span>
<span class="o">|&gt;</span> <span onmouseout="hideTip(event, 'fs4', 8)" onmouseover="showTip(event, 'fs4', 8)" class="fn">printfn</span> <span class="s">&quot;</span><span class="pf">%s</span><span class="s">&quot;</span>

<span class="c">// Instantiating a New Type</span>
<span class="c">//       </span>
<span class="c">// Generally, there are two ways that an</span>
<span class="c">//       </span>
<span class="c">// instance of a class or structure can</span>
<span class="c">//       </span>
<span class="c">// be instntiated.</span>
</code></pre></td>
</tr>
</table>
<h3><a name="replace-with-MatchEvaluator" class="anchor" href="#replace-with-MatchEvaluator">replace with MatchEvaluator</a></h3>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l"> 1: </span>
<span class="l"> 2: </span>
<span class="l"> 3: </span>
<span class="l"> 4: </span>
<span class="l"> 5: </span>
<span class="l"> 6: </span>
<span class="l"> 7: </span>
<span class="l"> 8: </span>
<span class="l"> 9: </span>
<span class="l">10: </span>
<span class="l">11: </span>
<span class="l">12: </span>
<span class="l">13: </span>
<span class="l">14: </span>
<span class="l">15: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="k">let</span> <span onmouseout="hideTip(event, 'fs7', 9)" onmouseover="showTip(event, 'fs7', 9)" class="fn">capText</span> <span class="pn">(</span><span onmouseout="hideTip(event, 'fs8', 10)" onmouseover="showTip(event, 'fs8', 10)" class="id">m</span> <span class="pn">:</span> <span onmouseout="hideTip(event, 'fs9', 11)" onmouseover="showTip(event, 'fs9', 11)" class="rt">Match</span><span class="pn">)</span> <span class="o">=</span>
    <span class="c">// Get the matched string.</span>
    <span class="k">let</span> <span onmouseout="hideTip(event, 'fs10', 12)" onmouseover="showTip(event, 'fs10', 12)" class="id">x</span> <span class="o">=</span> <span onmouseout="hideTip(event, 'fs8', 13)" onmouseover="showTip(event, 'fs8', 13)" class="id">m</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs11', 14)" onmouseover="showTip(event, 'fs11', 14)" class="id">Value</span>
    <span class="c">// If the first char is lower case...</span>
    <span class="k">if</span> <span class="pn">(</span><span onmouseout="hideTip(event, 'fs12', 15)" onmouseover="showTip(event, 'fs12', 15)" class="vt">Char</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs13', 16)" onmouseover="showTip(event, 'fs13', 16)" class="id">IsLower</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs10', 17)" onmouseover="showTip(event, 'fs10', 17)" class="id">x</span><span class="m">.</span><span class="pn">[</span><span class="n">0</span><span class="pn">]</span><span class="pn">)</span><span class="pn">)</span> <span class="k">then</span>
        <span class="c">// Capitalize it.</span>
        <span class="pn">(</span><span onmouseout="hideTip(event, 'fs12', 18)" onmouseover="showTip(event, 'fs12', 18)" class="vt">Char</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs14', 19)" onmouseover="showTip(event, 'fs14', 19)" class="id">ToUpper</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs10', 20)" onmouseover="showTip(event, 'fs10', 20)" class="id">x</span><span class="m">.</span><span class="pn">[</span><span class="n">0</span><span class="pn">]</span><span class="pn">)</span><span class="pn">.</span><span class="id">ToString</span><span class="pn">(</span><span class="pn">)</span><span class="pn">)</span> <span class="o">+</span> <span class="pn">(</span><span onmouseout="hideTip(event, 'fs10', 21)" onmouseover="showTip(event, 'fs10', 21)" class="fn">x</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs15', 22)" onmouseover="showTip(event, 'fs15', 22)" class="id">Substring</span><span class="pn">(</span><span class="n">1</span><span class="pn">,</span> <span onmouseout="hideTip(event, 'fs10', 23)" onmouseover="showTip(event, 'fs10', 23)" class="id">x</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs16', 24)" onmouseover="showTip(event, 'fs16', 24)" class="id">Length</span> <span class="o">-</span> <span class="n">1</span><span class="pn">)</span><span class="pn">)</span>
    <span class="k">else</span>
        <span onmouseout="hideTip(event, 'fs10', 25)" onmouseover="showTip(event, 'fs10', 25)" class="id">x</span>

<span class="s">&quot;four score and seven years ago&quot;</span>
<span class="o">|&gt;</span> <span class="id">FsRegEx</span><span class="pn">.</span><span class="id">replaceByMatch</span> <span class="s">@&quot;\w+&quot;</span> <span class="pn">(</span><span class="k">new</span> <span onmouseout="hideTip(event, 'fs17', 26)" onmouseover="showTip(event, 'fs17', 26)" class="id">MatchEvaluator</span><span class="pn">(</span><span onmouseout="hideTip(event, 'fs7', 27)" onmouseover="showTip(event, 'fs7', 27)" class="id">capText</span><span class="pn">)</span><span class="pn">)</span>
<span class="o">|&gt;</span> <span onmouseout="hideTip(event, 'fs4', 28)" onmouseover="showTip(event, 'fs4', 28)" class="fn">printfn</span> <span class="s">&quot;result=[</span><span class="pf">%s</span><span class="s">]&quot;</span>

<span class="c">// result=[Four Score And Seven Years Ago]</span>
</code></pre></td>
</tr>
</table>
<h2><a name="Splitting" class="anchor" href="#Splitting">Splitting</a></h2>
<p>The <code>System.Text.RegularExpressions.Regex</code> Class provides several overloads of <code>split</code> to split strings into arrays. The same overloads are available in the <code>FsRegEx</code> class and as individually
named functions, including overloads and functions using <code>RegexOptions</code>. <code>Timeout</code> is not supported at this time.</p>
<h3><a name="split-with-Regex-option" class="anchor" href="#split-with-Regex-option">split with Regex option</a></h3>
<table class="pre"><tr><td class="lines"><pre class="fssnip"><span class="l">1: </span>
<span class="l">2: </span>
<span class="l">3: </span>
<span class="l">4: </span>
<span class="l">5: </span>
<span class="l">6: </span>
<span class="l">7: </span>
<span class="l">8: </span>
<span class="l">9: </span>
</pre></td>
<td class="snippet"><pre class="fssnip highlighted"><code lang="fsharp"><span class="s">&quot;Abc1234Def5678Ghi9012Jklm&quot;</span>
<span class="o">|&gt;</span> <span class="id">FsRegEx</span><span class="pn">.</span><span class="id">splitOpt</span> <span class="s">&quot;[a-z]+&quot;</span> <span onmouseout="hideTip(event, 'fs5', 29)" onmouseover="showTip(event, 'fs5', 29)" class="id">RegexOptions</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs18', 30)" onmouseover="showTip(event, 'fs18', 30)" class="id">IgnoreCase</span>
<span class="o">|&gt;</span> <span onmouseout="hideTip(event, 'fs19', 31)" onmouseover="showTip(event, 'fs19', 31)" class="m">Array</span><span class="pn">.</span><span onmouseout="hideTip(event, 'fs20', 32)" onmouseover="showTip(event, 'fs20', 32)" class="id">iter</span> <span class="pn">(</span><span class="k">fun</span> <span onmouseout="hideTip(event, 'fs10', 33)" onmouseover="showTip(event, 'fs10', 33)" class="id">x</span> <span class="k">-&gt;</span> <span onmouseout="hideTip(event, 'fs4', 34)" onmouseover="showTip(event, 'fs4', 34)" class="fn">printfn</span> <span class="s">&quot;&#39;</span><span class="pf">%s</span><span class="s">&#39;&quot;</span> <span onmouseout="hideTip(event, 'fs10', 35)" onmouseover="showTip(event, 'fs10', 35)" class="id">x</span><span class="pn">)</span>

<span class="c">// &#39;&#39;</span>
<span class="c">// &#39;1234&#39;</span>
<span class="c">// &#39;5678&#39;</span>
<span class="c">// &#39;9012&#39;</span>
<span class="c">// &#39;&#39;</span>
</code></pre></td>
</tr>
</table>

<div class="tip" id="fs1">namespace System</div>
<div class="tip" id="fs2">namespace System.Text</div>
<div class="tip" id="fs3">namespace System.Text.RegularExpressions</div>
<div class="tip" id="fs4">val printfn : format:Printf.TextWriterFormat&lt;&#39;T&gt; -&gt; &#39;T</div>
<div class="tip" id="fs5">type RegexOptions =<br />&#160;&#160;| None = 0<br />&#160;&#160;| IgnoreCase = 1<br />&#160;&#160;| Multiline = 2<br />&#160;&#160;| ExplicitCapture = 4<br />&#160;&#160;| Compiled = 8<br />&#160;&#160;| Singleline = 16<br />&#160;&#160;| IgnorePatternWhitespace = 32<br />&#160;&#160;| RightToLeft = 64<br />&#160;&#160;| ECMAScript = 256<br />&#160;&#160;| CultureInvariant = 512</div>
<div class="tip" id="fs6">field RegexOptions.Multiline: RegexOptions = 2</div>
<div class="tip" id="fs7">val capText : m:Match -&gt; string</div>
<div class="tip" id="fs8">val m : Match</div>
<div class="tip" id="fs9">type Match =<br />&#160;&#160;inherit Group<br />&#160;&#160;member Groups : GroupCollection<br />&#160;&#160;member NextMatch : unit -&gt; Match<br />&#160;&#160;member Result : replacement:string -&gt; string<br />&#160;&#160;static member Empty : Match<br />&#160;&#160;static member Synchronized : inner:Match -&gt; Match</div>
<div class="tip" id="fs10">val x : string</div>
<div class="tip" id="fs11">property Capture.Value: string</div>
<div class="tip" id="fs12">type Char =<br />&#160;&#160;struct<br />&#160;&#160;&#160;&#160;member CompareTo : value:obj -&gt; int + 1 overload<br />&#160;&#160;&#160;&#160;member Equals : obj:obj -&gt; bool + 1 overload<br />&#160;&#160;&#160;&#160;member GetHashCode : unit -&gt; int<br />&#160;&#160;&#160;&#160;member GetTypeCode : unit -&gt; TypeCode<br />&#160;&#160;&#160;&#160;member ToString : unit -&gt; string + 1 overload<br />&#160;&#160;&#160;&#160;static val MaxValue : char<br />&#160;&#160;&#160;&#160;static val MinValue : char<br />&#160;&#160;&#160;&#160;static member ConvertFromUtf32 : utf32:int -&gt; string<br />&#160;&#160;&#160;&#160;static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -&gt; int + 1 overload<br />&#160;&#160;&#160;&#160;static member GetNumericValue : c:char -&gt; float + 1 overload<br />&#160;&#160;&#160;&#160;...<br />&#160;&#160;end</div>
<div class="tip" id="fs13">Char.IsLower(c: char) : bool<br />Char.IsLower(s: string, index: int) : bool</div>
<div class="tip" id="fs14">Char.ToUpper(c: char) : char<br />Char.ToUpper(c: char, culture: Globalization.CultureInfo) : char</div>
<div class="tip" id="fs15">String.Substring(startIndex: int) : string<br />String.Substring(startIndex: int, length: int) : string</div>
<div class="tip" id="fs16">property String.Length: int</div>
<div class="tip" id="fs17">type MatchEvaluator =<br />&#160;&#160;delegate of Match -&gt; string</div>
<div class="tip" id="fs18">field RegexOptions.IgnoreCase: RegexOptions = 1</div>
<div class="tip" id="fs19">type Array =<br />&#160;&#160;member Clone : unit -&gt; obj<br />&#160;&#160;member CopyTo : array:Array * index:int -&gt; unit + 1 overload<br />&#160;&#160;member GetEnumerator : unit -&gt; IEnumerator<br />&#160;&#160;member GetLength : dimension:int -&gt; int<br />&#160;&#160;member GetLongLength : dimension:int -&gt; int64<br />&#160;&#160;member GetLowerBound : dimension:int -&gt; int<br />&#160;&#160;member GetUpperBound : dimension:int -&gt; int<br />&#160;&#160;member GetValue : [&lt;ParamArray&gt;] indices:int[] -&gt; obj + 7 overloads<br />&#160;&#160;member Initialize : unit -&gt; unit<br />&#160;&#160;member IsFixedSize : bool<br />&#160;&#160;...</div>
<div class="tip" id="fs20">val iter : action:(&#39;T -&gt; unit) -&gt; array:&#39;T [] -&gt; unit</div>

        </div>
        <div class="span3">
          <img src="/FsRegEx/img/logo.png" alt="F# Project" style="width:150px;margin:10px" />  
            <ul class="nav nav-list" id="menu" style="margin-top: 20px;">
                <li class="nav-header">FsRegEx</li>
                <li><a href="/FsRegEx/index.html">Home page</a></li>
                <li class="divider"></li>
                <li><a href="http://nuget.org/packages/FsRegEx">Get Library via NuGet</a></li>
                <li><a href="https://github.com/jackfoxy/FsRegEx">Source Code on GitHub</a></li>
                <li><a href="/FsRegEx/license.html">License</a></li>
                <li><a href="/FsRegEx/release-notes.html">Release Notes</a></li>

                <li class="nav-header">Examples</li>
                <li><a href="/FsRegEx/matching.html">Matching</a></li>
                <li><a href="/FsRegEx/group.html">Match Groups</a></li>
                <li><a href="/FsRegEx/replacesplit.html">Replace and Split</a></li>
                <li><a href="/FsRegEx/capture.html">Capture</a></li>

                <li class="nav-header">Documentation</li>
                <li><a href="/FsRegEx/reference/index.html">API Reference</a></li>

                <li class="nav-header">More...</li>
                <li><a href="/FsRegEx/emailregex.html">On the Futility of Email Regex Validation</a></li>
                <li><a href="/FsRegEx/tutorial.html">F# Verbal Expressions Tutorial</a></li>
            </ul>
        </div>
      </div>
    </div>
    <a href="https://github.com/jackfoxy/FsRegEx"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"/></a>
  </body>
  </html>
