4 people like it.

Regex.Replace with a MatchEvaluator

An example of setting up a MatchEvaluator for use with Regex.Replace in F#.

1: 
2: 
3: 
4: 
5: 
6: 
let objRegex(pattern:string)(input:string)(replacement:MatchEvaluator) =
   Regex.Replace(input,pattern,replacement)

let objRegexEvaluator = MatchEvaluator(fun m -> String.concat "" ["""<span class="special-fs">""";m.Value;"</span>"])
    
objRegex("(?<![A-Za-z1-9])(Seq|List|Array|float|int)(?![A-Za-z1-9])")("NewtonRahpson(f:float-&gt;float)(fDiff:float-&gt;float)(a0:float)")(objRegexEvaluator)
val objRegex : pattern:string -> input:string -> replacement:'a -> 'b

Full name: Script.objRegex
val pattern : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val input : string
val replacement : 'a
val objRegexEvaluator : obj

Full name: Script.objRegexEvaluator
module String

from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
Raw view Test code New version

More information

Link:http://fssnip.net/7VJ
Posted:5 years ago
Author:stubel
Tags: regex