0 people like it.

Parse and replace string with templating characters

Starts at a given position. Using any kind of "tag" like <%replaceThis%> with something corresponding from the Map.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
let parseAndReplaceTemplate
    (startPosition: int)
    (startTag: string)
    (endTag: string)
    (replacements: Map<string, string>)
    (template: string)
    : string =
    // sanity checks (do once)
    if System.String.IsNullOrWhiteSpace startTag
       || System.String.IsNullOrWhiteSpace endTag
       || System.String.IsNullOrWhiteSpace template
       || replacements.IsEmpty
       || startPosition > (template.Length - 1) then
        template // return unaltered
    else
        let regexStr = startTag + "(.*?)" + endTag
        let regex = System.Text.RegularExpressions.Regex (regexStr)

        let rec replace (input: string) =
            let regexMatch = regex.Match input

            if regexMatch.Success then
                let key = regexMatch.Groups.[1].Value
                let value = replacements |> Map.tryFind key

                match value with
                | Some v ->
                    let before = input.Substring (0, regexMatch.Index)
                    let after = input.Substring (regexMatch.Index + regexMatch.Length)
                    before + v + replace after
                | None -> replace input
            else
                input

        let skippedText = template.Substring (0, startPosition)
        let output = replace (template.Substring startPosition)

        skippedText + output
val parseAndReplaceTemplate : startPosition:int -> startTag:string -> endTag:string -> replacements:Map<string,string> -> template:string -> string
val startPosition : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
val startTag : string
Multiple items
val string : value:'T -> string

--------------------
type string = System.String
val endTag : string
val replacements : Map<string,string>
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IReadOnlyDictionary<'Key,'Value>
  interface IReadOnlyCollection<KeyValuePair<'Key,'Value>>
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  ...

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val template : string
namespace System
Multiple items
type String =
  new : value:char[] -> string + 8 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool + 3 overloads
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 3 overloads
  member EnumerateRunes : unit -> StringRuneEnumerator
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  ...

--------------------
System.String(value: char []) : System.String
System.String(value: nativeptr<char>) : System.String
System.String(value: nativeptr<sbyte>) : System.String
System.String(value: System.ReadOnlySpan<char>) : System.String
System.String(c: char, count: int) : System.String
System.String(value: char [], startIndex: int, length: int) : System.String
System.String(value: nativeptr<char>, startIndex: int, length: int) : System.String
System.String(value: nativeptr<sbyte>, startIndex: int, length: int) : System.String
System.String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: System.Text.Encoding) : System.String
System.String.IsNullOrWhiteSpace(value: string) : bool
property Map.IsEmpty: bool with get
property System.String.Length: int with get
val regexStr : string
val regex : System.Text.RegularExpressions.Regex
namespace System.Text
namespace System.Text.RegularExpressions
Multiple items
type Regex =
  new : pattern:string -> Regex + 2 overloads
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member MatchTimeout : TimeSpan
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  ...

--------------------
System.Text.RegularExpressions.Regex(pattern: string) : System.Text.RegularExpressions.Regex
System.Text.RegularExpressions.Regex(pattern: string, options: System.Text.RegularExpressions.RegexOptions) : System.Text.RegularExpressions.Regex
System.Text.RegularExpressions.Regex(pattern: string, options: System.Text.RegularExpressions.RegexOptions, matchTimeout: System.TimeSpan) : System.Text.RegularExpressions.Regex
val replace : (string -> string)
val input : string
val regexMatch : System.Text.RegularExpressions.Match
System.Text.RegularExpressions.Regex.Match(input: string) : System.Text.RegularExpressions.Match
System.Text.RegularExpressions.Regex.Match(input: string, startat: int) : System.Text.RegularExpressions.Match
System.Text.RegularExpressions.Regex.Match(input: string, beginning: int, length: int) : System.Text.RegularExpressions.Match
property System.Text.RegularExpressions.Group.Success: bool with get
val key : string
property System.Text.RegularExpressions.Match.Groups: System.Text.RegularExpressions.GroupCollection with get
val value : string option
val tryFind : key:'Key -> table:Map<'Key,'T> -> 'T option (requires comparison)
union case Option.Some: Value: 'T -> Option<'T>
val v : string
val before : string
System.String.Substring(startIndex: int) : string
System.String.Substring(startIndex: int, length: int) : string
property System.Text.RegularExpressions.Capture.Index: int with get, set
val after : string
property System.Text.RegularExpressions.Capture.Length: int with get, set
union case Option.None: Option<'T>
val skippedText : string
val output : string
Raw view Test code New version

More information

Link:http://fssnip.net/88K
Posted:2 years ago
Author:shazmodan
Tags: string manipulation , string matching