howl.Regex

.pattern holds the regex used for construction

assert.equal 'foo(bar)', r('foo(bar)').pattern

.capture_count holds the number of captures in the pattern

assert.equal 2, r('foo(bar) (\\w+)').capture_count

r.is_instance(v) returns true if <v> is a regex

assert.is_true r.is_instance r'foo'
assert.is_false r.is_instance 'foo'
assert.is_false r.is_instance {}

escape(s) returns a string with all special regular expression symbols escaped

assert.equal 'a\\.b\\*c', r.escape 'a.b*c'

tostring(regex) returns the pattern

assert.equal '\\s*(foo)', tostring r'\\s*(foo)'

(creation)

raises an error if the pattern is invalid

assert.raises 'regular expression', -> r'?\\'

returns a regex for a valid pattern

assert.is_not_nil r'foo()\\d+'

accepts a regex as well

assert.is_not_nil r r'foo()\\d+'

accepts and optional table of compile flags

reg = r '.', {r.DOTALL}
assert.is_truthy reg\match "\n"

accepts and optional table of match flags

reg = r 'x', nil, {r.MATCH_ANCHORED}
assert.is_falsy reg\match "ax"

match(string [, init])

returns nil if the pattern does not match

assert.is_nil r'foo'\match 'bar'

(with no captures in the pattern)

returns the entire match

assert.equal 'right', r'ri\\S+'\match 'red right hand'

(with captures in the pattern)

returns the captured values

assert.same { 'red', 'right' }, { r'(r\\w+)\\s+(\\S+)'\match 'red right hand' }

empty captures are returned as position captures

assert.same { 1, 4 }, { r'()red()'\match 'red' }

position captures are character based

assert.same { 2, 3 }, { r'å()ä()'\match 'åäö' }

return non-matching optional captures as empty strings

assert.same { '1', '', '2' }, { r'(1)(\\w)?(2)'\match '12' }

(when init is specified)

matching starts from the init position

assert.equal 'right', r'r\\S+'\match 'red right hand', 2
assert.equal 6, r'r()\\S+'\match 'red right hand', 2

negative values counts from the end

assert.equal 'og', r'o\\w'\match 'top dog', -2

find(s, init)

returns nil if the pattern could not be found in <s>

assert.is_nil r'foo'\find 'bar'

returns the indices where the pattern match starts and end if found

assert.same { 2, 2 }, { r'\\pL'\find '!äö' }

returns any captures after the indices

assert.same { 2, 2, 'ä' }, { r'(\\pL)'\find '!äö' }

empty captures are returned as position captures

assert.same { 2, 2, 3 }, { r'\\pL()'\find '!äö' }

starts matching after init

assert.same { 3, 5, 6 }, { r'\\w+()'\find '12ab2', 3}

gmatch(s)

returns no matches when it does not match

matches = [m for m in r'\\d+'\gmatch 'well hello there']
assert.same {}, matches

(with no captures in the pattern)

produces each consecutive match in each call

matches = [m for m in r'\\w+'\gmatch 'well hello there']
assert.same { 'well', 'hello', 'there' }, matches

(with captures in the pattern)

returns empty captures as position matches

matches = [p for p in r'()\\pL+'\gmatch 'well hellö there' ]
assert.same { 1, 6, 12 }, matches

produces the the set of captures in each call

matches = [{p,m} for p,m in r'()(\\w+)'\gmatch 'well hello there']
assert.same { {1, 'well'}, {6, 'hello'}, {12, 'there'} }, matches

test(s)

returns true if the regex matches <s>

assert.is_true r('ri\\S+')\test 'red right hand'

returns false if the regex does not match <s>

assert.is_false r('foo')\test 'bar'