FSharpVerbalExpressions


Matching

IsMatch

The System.Text.RegularExpressions.Regex Class provides several overloads of isMatch. 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.

is match on first occurence

 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: 
open FsVerbalExpressions
module F = FsRegEx
module V = VerbalExpression

let partNumbers = [| "1298-673-4192"; "A08Z-931-468A"; 
                    "_A90-123-129X"; "12345-KKA-1230"; 
                    "0919-2893-1256" |]

for partNumber in partNumbers do
    printfn "%s %s a valid part number."
        partNumber
        (if (F.isMatch @"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$" partNumber ) then "is" 
            else "is not")

let verbEx = new V.VerbEx(@"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$")

for partNumber in partNumbers do
    printfn "%s %s a valid part number."
        partNumber
        (if (verbEx.IsMatch(partNumber)) then "is" 
            else "is not")


// 1298-673-4192 is a valid part number.
// A08Z-931-468A is a valid part number.
// _A90-123-129X is not a valid part number.
// 12345-KKA-1230 is not a valid part number.
// 0919-2893-1256 is not a valid part number.

is match starting at position

 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: 
let labeledPartNumbers = [| "Part Number: 1298-673-4192"; "Part No: A08Z-931-468A"; 
                        "_A90-123-129X"; "123K-000-1230"; 
                        "SKU: 0919-2893-1256" |]

for partNumber in labeledPartNumbers do
    let start = partNumber.IndexOf(':')
    if (start >= 0) then
        printfn "%s %s a valid part number."
            partNumber
            (if (F.isMatchAt @"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$" start partNumber) then "is" else "is not")
    else
        printfn "Cannot find starting position in %s." partNumber

for partNumber in labeledPartNumbers do
    let start = partNumber.IndexOf(':')
    if (start >= 0) then
        printfn "%s %s a valid part number."
            partNumber
            (if (verbEx.IsMatch(partNumber, start)) then "is" else "is not")
    else
        printfn "Cannot find starting position in %s." partNumber

// Part Number: 1298-673-4192 is a valid part number.
// Part No: A08Z-931-468A is a valid part number.
// Cannot find starting position in _A90-123-129X.
// Cannot find starting position in 123K-000-1230.
// SKU: 0919-2893-1256 is not a valid part number.

FsMatch

FsMatch provides the functionality in the System.Text.RegularExpressions.Match Class, but returning arrays of objects instead of special collections.

iterate over matches

1: 
2: 
3: 
4: 
5: 
6: 
"Who writes these notes?"
|> FsRegEx.matches @"\b\w+es\b"
|> Array.iter (fun m -> printfn "Found '%s' at position %i" m.Value m.Index)

// Found 'writes' at position 4
// Found 'notes' at position 17

first match and iteration

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
let regExpr = @"\b\w+es\b"
let sentence = "Who writes these notes and uses our paper?"

let m = FsRegEx.firstMatch regExpr sentence

if m.Success then
    printfn "Found first 'es' in '%s' at position %i" m.Value m.Index
    sentence
    |> FsRegEx.matchesAt regExpr (m.Index + m.Length)
    |> Array.iter (fun m -> printfn "Also found '%s' at position %i" m.Value m.Index)

// Found first 'es' in 'writes' at position 4
// Also found 'notes' at position 17
// Also found 'uses' at position 27
namespace FsVerbalExpressions
module FsRegEx

from FsVerbalExpressions
module VerbalExpression

from FsVerbalExpressions
val partNumbers : string []

Full name: Matching.partNumbers
val partNumber : string
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val isMatch : regularExpression:string -> input:string -> bool

Full name: FsVerbalExpressions.FsRegEx.isMatch
val verbEx : VerbalExpression.VerbEx

Full name: Matching.verbEx
Multiple items
type VerbEx =
  new : unit -> VerbEx
  new : regularExpression:string -> VerbEx
  new : regexOptions:RegexOptions -> VerbEx
  new : regularExpression:string * regexOptions:RegexOptions -> VerbEx
  new : regularExpression:string * regexOptions:RegexOptions * matchTimeout:TimeSpan -> VerbEx
  member Capture : input:string -> string -> string
  member GroupNameFromNumber : n:int -> string option
  member GroupNames : unit -> string array
  member GroupNumberFromName : groupName:string -> int option
  member GroupNumbers : unit -> int array
  ...

Full name: FsVerbalExpressions.VerbalExpression.VerbEx

--------------------
new : unit -> VerbalExpression.VerbEx
new : regexOptions:System.Text.RegularExpressions.RegexOptions -> VerbalExpression.VerbEx
new : regularExpression:string -> VerbalExpression.VerbEx
new : regularExpression:string * regexOptions:System.Text.RegularExpressions.RegexOptions -> VerbalExpression.VerbEx
new : regularExpression:string * regexOptions:System.Text.RegularExpressions.RegexOptions * matchTimeout:System.TimeSpan -> VerbalExpression.VerbEx
member VerbalExpression.VerbEx.IsMatch : input:string -> bool
member VerbalExpression.VerbEx.IsMatch : input:string * startAt:int -> bool
val labeledPartNumbers : string []

Full name: Matching.labeledPartNumbers
val start : int
System.String.IndexOf(value: string) : int
System.String.IndexOf(value: char) : int
System.String.IndexOf(value: string, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int) : int
System.String.IndexOf(value: char, startIndex: int) : int
System.String.IndexOf(value: string, startIndex: int, comparisonType: System.StringComparison) : int
System.String.IndexOf(value: string, startIndex: int, count: int) : int
System.String.IndexOf(value: char, startIndex: int, count: int) : int
System.String.IndexOf(value: string, startIndex: int, count: int, comparisonType: System.StringComparison) : int
val isMatchAt : regularExpression:string -> startAt:int -> input:string -> bool

Full name: FsVerbalExpressions.FsRegEx.isMatchAt
val matches : regularExpression:string -> input:string -> FsMatch []

Full name: FsVerbalExpressions.FsRegEx.matches
module Array

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> array:'T [] -> unit

Full name: Microsoft.FSharp.Collections.Array.iter
val m : FsMatch
property FsMatch.Value: string
property FsMatch.Index: int
val regExpr : string

Full name: Matching.regExpr
val sentence : string

Full name: Matching.sentence
val m : FsMatch

Full name: Matching.m
val firstMatch : regularExpression:string -> input:string -> FsMatch

Full name: FsVerbalExpressions.FsRegEx.firstMatch
property FsMatch.Success: bool
val matchesAt : regularExpression:string -> startAt:int -> input:string -> FsMatch []

Full name: FsVerbalExpressions.FsRegEx.matchesAt
property FsMatch.Length: int
Fork me on GitHub