FsRegEx


Replacing and Splitting

Replace

The System.Text.RegularExpressions.Regex Class provides several overloads of replace. The same overloads are available in the FsRegEx class and as individually named functions, 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: 
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 FsRegEx class and as individually named functions, 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 System
namespace System.Text
namespace System.Text.RegularExpressions
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
type RegexOptions =
  | None = 0
  | IgnoreCase = 1
  | Multiline = 2
  | ExplicitCapture = 4
  | Compiled = 8
  | Singleline = 16
  | IgnorePatternWhitespace = 32
  | RightToLeft = 64
  | ECMAScript = 256
  | CultureInvariant = 512
field RegexOptions.Multiline: RegexOptions = 2
val capText : m:Match -> string
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
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
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
type MatchEvaluator =
  delegate of Match -> string
field RegexOptions.IgnoreCase: RegexOptions = 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
  ...
val iter : action:('T -> unit) -> array:'T [] -> unit
Fork me on GitHub