22 people like it.

Regex Match Index

A function for interpreting the zero-based index property of a successful regular expression match in terms of line and column numbers.

 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: 
open System.Text.RegularExpressions

let matchPosition str pattern =
    let m = Regex(pattern).Match(str)
    match m.Success with
    | true ->
        let index = m.Index
        let lines = str.Split '\n'
        let rec getLineCol idx len =
            let l = lines.[idx].Length + 1
            let len' = l + len
            if len' > index then
                idx + 1, l - (len' - index) + 1
            else
                getLineCol (idx + 1) len'   
        
        let lineCol = getLineCol 0 0
        Some lineCol
    | false -> None

// Example
let text = "This is a test."
let position = matchPosition text "a"
match position with
| Some (line, col) -> printfn "Match found in line %d, column %d." line col
| None -> ()

// Output:
// Match found in line 1, column 9.
// val text : string = "This is a test."
// val position : (int * int) option = Some (1, 9)
namespace System
namespace System.Text
namespace System.Text.RegularExpressions
val matchPosition : str:string -> pattern:string -> (int * int) option

Full name: Script.matchPosition
val str : string
val pattern : string
val m : Match
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  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 Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
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
property Group.Success: bool
val index : int
property Capture.Index: int
val lines : string []
System.String.Split([<System.ParamArray>] separator: char []) : string []
System.String.Split(separator: string [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int) : string []
System.String.Split(separator: string [], count: int, options: System.StringSplitOptions) : string []
System.String.Split(separator: char [], count: int, options: System.StringSplitOptions) : string []
val getLineCol : (int -> int -> int * int)
val idx : int
val len : int
val l : int
val len' : int
val lineCol : int * int
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val text : string

Full name: Script.text
val position : (int * int) option

Full name: Script.position
val line : int
val col : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/1w
Posted:13 years ago
Author:Taha Hachana
Tags: regular , expressions , regex