FSharpVerbalExpressions


Replacing and Splitting

Replace

The System.Text.RegularExpressions.Regex Class provides several overloads of replace. The same overloads are available in the VerbEx class and as individually named functions in the FsRegEx module, including overloads and functions using RegexOptions. Timeout is not supported at this time.

simple replacement of all occurence

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
open FsVerbalExpressions
open System
open System.Text.RegularExpressions

"This is   text with   far  too   much   " + 
                "whitespace."
|> FsRegEx.replace "\\s+" " "
|>printfn "Replacement String: %s" 

// Replacement String: This is text with far too much whitespace.

replace max time starting at

Replaces a specified maximum number of strings starting at location that match a regular expression pattern with a specified replacement string.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
"Instantiating a New Type\n" +
    "Generally, there are two ways that an\n" + 
    "instance of a class or structure can\n" +
    "be instantiated. "
|> FsRegEx.replaceMaxTimesStartAtOpt "^.*$" RegexOptions.Multiline "\n$&"  -1 1
|> printfn "%s"

// Instantiating a New Type
//       
// Generally, there are two ways that an
//       
// instance of a class or structure can
//       
// be instntiated.

replace with MatchEvaluator

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
let capText (m : Match) =
    // Get the matched string.
    let x = m.Value
    // If the first char is lower case...
    if (Char.IsLower(x.[0])) then
        // Capitalize it.
        (Char.ToUpper(x.[0]).ToString()) + (x.Substring(1, x.Length - 1))
    else
        x

"four score and seven years ago"
|> FsRegEx.replaceByMatch @"\w+" (new MatchEvaluator(capText))
|> printfn "result=[%s]"

// result=[Four Score And Seven Years Ago]

Splitting

The System.Text.RegularExpressions.Regex Class provides several overloads of split to split strings into arrays. The same overloads are available in the VerbEx class and as individually named functions in the FsRegEx module, including overloads and functions using RegexOptions. Timeout is not supported at this time.

split with Regex option

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
"Abc1234Def5678Ghi9012Jklm"
|> FsRegEx.splitOpt "[a-z]+" RegexOptions.IgnoreCase
|> Array.iter (fun x -> printfn "'%s'" x)

// ''
// '1234'
// '5678'
// '9012'
// ''
namespace FsVerbalExpressions
namespace System
namespace System.Text
namespace System.Text.RegularExpressions
module FsRegEx

from FsVerbalExpressions
val replace : regularExpression:string -> replacement:string -> input:string -> string

Full name: FsVerbalExpressions.FsRegEx.replace
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val replaceMaxTimesStartAtOpt : regularExpression:string -> regexOptions:RegexOptions -> replacement:string -> count:int -> startAt:int -> input:string -> string

Full name: FsVerbalExpressions.FsRegEx.replaceMaxTimesStartAtOpt
type RegexOptions =
  | None = 0
  | IgnoreCase = 1
  | Multiline = 2
  | ExplicitCapture = 4
  | Compiled = 8
  | Singleline = 16
  | IgnorePatternWhitespace = 32
  | RightToLeft = 64
  | ECMAScript = 256
  | CultureInvariant = 512

Full name: System.Text.RegularExpressions.RegexOptions
field RegexOptions.Multiline = 2
val capText : m:Match -> string

Full name: Replacesplit.capText
val m : Match
type Match =
  inherit Group
  member Groups : GroupCollection
  member NextMatch : unit -> Match
  member Result : replacement:string -> string
  static member Empty : Match
  static member Synchronized : inner:Match -> Match

Full name: System.Text.RegularExpressions.Match
val x : string
property Capture.Value: string
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Char.IsLower(c: char) : bool
Char.IsLower(s: string, index: int) : bool
Char.ToUpper(c: char) : char
Char.ToUpper(c: char, culture: Globalization.CultureInfo) : char
String.Substring(startIndex: int) : string
String.Substring(startIndex: int, length: int) : string
property String.Length: int
val replaceByMatch : regularExpression:string -> evalutor:MatchEvaluator -> input:string -> string

Full name: FsVerbalExpressions.FsRegEx.replaceByMatch
type MatchEvaluator =
  delegate of Match -> string

Full name: System.Text.RegularExpressions.MatchEvaluator
val splitOpt : regularExpression:string -> regexOptions:RegexOptions -> input:string -> string array

Full name: FsVerbalExpressions.FsRegEx.splitOpt
field RegexOptions.IgnoreCase = 1
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val iter : action:('T -> unit) -> array:'T [] -> unit

Full name: Microsoft.FSharp.Collections.Array.iter
Fork me on GitHub