package lua_pattern

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type t

Implementation of Lua patterns. Patterns are a simplified regex meant to be easier to implement and use.

module Capture : sig ... end

Captures are ranges that have been matched in the search string.

module Match : sig ... end

Matches that the pattern has matched into the search strings.

val of_string : string -> t option

Compile a pattern.

  • returns

    None if the pattern is invalid, Some t if the pattern is valid.

val find : ?start:int -> string -> t -> (int * int) option

Find the first successful pattern hit in the search string.

  • parameter start

    optional place in the search string to start searching

  • returns

    None if the pattern is not found, Some (s, e) if the pattern was found, where the range is [s, e)

val mtch : ?start:int -> string -> t -> Match.t option

Match a pattern in the search string.

  • parameter start

    optional place in the search string to start

  • returns

    None if the pattern was not found, Some m if the pattern was found.

val substitute : ?start:int -> s:string -> r:(Match.t -> string) -> t -> string option

Replace a matches in a string. Replacing is a function on the Match.t returning a new string.

  • parameter start

    optional place in the search string to start

  • parameter s

    string to search int

  • parameter r

    replace function

  • returns

    None if the pattern was not found, Some str where str is the string with replacements applied

val rep_str : string -> Match.t -> string

Helper function for the common case where substitute is to replace a pattern with a static string. rep_str also supports access captures with

%N

where

N

is a number from 1 to 9.

Example:

let pat = Option.value_exn (of_string "(%d+)(%a+)") in substitute ~s:"123foobar" ~r:(rep_str "%2%1") pat

yields

Some "foobar123"