6. Regular Expression

6.1. Regular expression format

The regular expression format depends on the regular expression module that is loaded. Any % found in the string will be automatically converted to a \.

Examples for the pcre module:

'[%r]?%n'
'[[:blank:]]+'
'[0-9]+%.[0-9]+'
'[^()<>@,;:%\\"/%[%]?={}[:blank:]]+'

6.2. Module

object regexp_module

Regexp module implementation.

<regexp_module>:match(pattern, string, options) → match, begin, end
Parameters:
  • pattern (string) – Regular expression pattern.
  • string (string) – String against which regular expression is matched.
  • options (number) – Regular expression compilation options.
Returns:
  • match (string) – Result of the matching.
  • begin (number) – Index in the string where the match begins.
  • end (number) – Index in the string where the match ends.

Match a regular expression against a given string.

<regexp_module>:match(pattern, buffer, options) → match
Parameters:
  • pattern (string) – Regular expression pattern.
  • buffer (vbuffer) – Buffer against which regular expression is matched.
  • options (number) – Regular expression compilation options.
Returns:
  • match (vbuffer_sub) – Matching sub-buffer or nil if no match

Match a regular expression against a given buffer.

<regexp_module>:match(pattern, buffer_iterator, options, createsub) → match
Parameters:
  • pattern (string) – Regular expression pattern.
  • buffer_iterator (vbuffer_iterator) – Buffer iterator against which the regular expression is matched.
  • options (number) – Regular expression compilation options.
  • createsub (boolean) – true if the function should build a sub-buffer.
Returns:
  • match (vbuffer_sub or boolean) – If createsub is true return matching sub-buffer or nil, otherwise return a boolean

Match a regular expression against a given buffer iterator.

Usage:

local match = pcre:match("%s+", iter, 0, true)
if math then print(match:asstring()) end
<regexp_module>:compile(pattern, options) → re
Parameters:
  • pattern (string) – Regular expression pattern.
  • options (number) – Regular expression compilation options.
Returns:
  • re (regexp) – A compiled regexp object.

Compile a regular expression.

<regexp_module>.CASE_INSENSITIVE

Compilation option setting regular expression case insensitive.

<regexp_module>.EXTENDED

Compilation option allowing to ignore white space chars

6.3. Compiled regular expression

object regexp

Compiled regular expression object.

<regexp>:match(string) → match, begin, end
Parameters:
  • string (string) – String against which regular expression is matched.
Returns:
  • match (string) – Result of the matching.
  • begin (number) – Index in the string where the match begins.
  • end (number) – Index in the string where the match ends.

Match the compiled regular expression against a given string.

<regexp>:match(buffer) → match
Parameters:
  • buffer (vbuffer) – Buffer against which the regular expression is matched.
Returns:
  • match (vbuffer_sub) – Matching sub-buffer or nil if no match

Match the compiled regular expression against a given buffer.

<regexp>:match(buffer_iterator, createsub = false[, readonly]) → match
Parameters:
  • buffer_iterator (vbuffer_iterator) – Buffer iterator against which the regular expression is matched.
  • createsub (boolean) – If true a sub buffer will be returned otherwise it is a boolean.
  • readonly (boolean) – Set to true if working on a read-only buffer.
Returns:
  • match (vbuffer_sub or boolean) – If createsub is true return matching sub-buffer or nil, otherwise return a boolean

Match the compiled regular expression against a given buffer iterator.

<regexp>:get_sink() → sink
Returns:

Create a regular expression context that can be used for matching the regular expression against chunks of data.

6.4. Regular expression sink

object regexp_sink

Sink that can be used to match a regular expression on data pieces by pieces.

<regexp_sink>:feed(string, eof[, result]) → match
Parameters:
  • string (string) – String against which the regular expression is matched.
  • eof (boolean) – true if this string is the last one.
  • result (regexp_result) – Data used to store the indices of the match.
Returns:
  • match (boolean) – Result of the matching.

Match the compiled regular expression across multiple strings.

<regexp_sink>:feed(buffer, eof) → match, begin, end
Parameters:
  • buffer (vbuffer_sub) – Buffer against which the regular expression is matched.
  • eof (boolean) – true if this string is the last one.
Returns:
  • match (boolean) – Result of the matching.
  • begin (vbuffer_iterator) – Position of the beginning of the match or nil.
  • end (vbuffer_iterator) – Position of the end of the match or nil.

Match the compiled regular expression across multiple buffers. The begin and end values allow to track the position of the match.

<regexp_sink>:ispartial() → partial
Returns:
  • match (boolean) – State of this sink.

Get the sink state. If something has start matching but more data are needed to be sure that it is a valid match then this function will return true.

object haka.regexp_result

A result for a string based matching.

<regexp_result>.offset
Type:number
<regexp_result>.size
Type:number

6.5. Example

-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.

local rem = require('regexp/pcre')

local re = rem.re:compile("foo")
local sink = re:create_sink()

local ret = sink:feed("some fo", false)
ret = sink:feed("o over two string", true)

print(ret)