Regular expressions
in LiveCode.
Logo sunny-tdz sunnYrex
a LiveCode library.

sunnYrex 1.14

From Thierry Douez
License:	compatible with indy/business LiveCode license
Version:	1.14 / 2019-12-17   Mode Wildcard or Regex
Version:	1.13 / 2019-12-06   add options to sunnYdumpArray
Version:	1.12 / 2019-11-26   add options to sunnYmatch
Version:	1.11 / 2019-11-19   small optimizations
Version:	1.1  / 2019-10-15   add sunnYmatchAll
Version:	1.0  / 2016-08-30   offical release
Version:	0.1  / 2013-06-01   private tool

sunnYmatch( aText, aRegex, Arr, N, options)

returns            true if any match, false otherwise
aText              the input text
aRegex             the regular expression
Arr                a multidimensional array of the form
  Arr[G][0]		position of start of match string of group G
  Arr[G][1]		position of end of match string of group  G
  Arr[G][2]		match string of group G
N                  number of groups (N pair of parentheses in regex)
Options            empty, "text", "chunk" or "both"
                   default (empty) set to "chunk"

Example:

put "a11e22gh" into string
put "(\d+)[^\d]*(\d+)" into aRegex

if sunnYmatch( string, aRegex, A, N, "both") then
    # number of groups: number of paired parentheses
	put N  # --> 2
	# start position of text in group 1
	put A[ 1][ 0]  # --> 2
	# end position of text in group 1
	put A[ 1][ 1]  # --> 3
	# matched text in group 1
	put A[ 1][ 2]  # --> 11
	# start position of text in group 2
	put A[ 2][ 0]  # --> 5
	# end position of text in group 2
	put A[ 2][ 1]  # --> 6
	# matched text in group 2
	put A[ 2][ 2]  # --> 22
	# group 0 is the whole match (no parentheses)
	# start position of text in group 0
	put A[ 0][ 0]  # --> 2
	# end position of text in group 0
	put A[ 0][ 1]  # --> 6
	# the whole match
	put A[ 0][ 2]  # --> 11e22
else
	put "No match!"
end if

sunnYmatchAll( aText, aRegex, Arr, N, option)

returns            true if any match, false otherwise
aText              the input text
aRegex             the regular expression
Arr                a multidimensional array of the form
  Arr[1][G][0]     char position of start of 1st match of group G
  Arr[1][G][1]     char position of end of 1st match of group  G
  Arr[1][G][2]     1st match of group G
N                  number of occurences found
option             "chunk", "text", "both" or empty
                   by default (empty) the option is set to "chunk"

Example:

put "a b c aa c bb" into string
put "(a+)|(b+)" into aRegex

if sunnYmatchAll( string, aRegex, A, N, "both") then
    # number of occurences found
	put N  # --> 4
	# View the resulting array:
	put sunnYdumpArray( A) # --> See below
end if

# Array A:
[1] ->
    [1] ->
		[0] -> 1
		[1] -> 1
		[2] -> a
[2] ->
	[2] ->
		[0] -> 3
		[1] -> 3
		[2] -> b
[3] ->
	[1] ->
		[0] -> 7
		[1] -> 8
		[2] -> aa
[4] ->
	[2] ->
		[0] -> 12
		[1] -> 13
		[2] -> bb

sunnYreplace( aText, aRegex, replaceText, resultText)

returns             true if any match, false otherwise
aText               the input text
aRegex              the regular expression
replaceText         will replace the matching part of string
resultText          the new string

Example 1:

put "123___567" into string
put "(_+)" into aRegex
put "4" into rText

if sunnYreplace( string, aRegex, rText, resultText) then
    put resultText # --> 1234567
end if

Example 2:

put "a c b d" into string
put "(c)\s(b)" into aRegex
put "\2\t\1" into rText

if sunnYreplace( string, aRegex, rText, resultText) then
	put resultText # --> a b c d
end if

Example 3:

put "a c b d" into string
put "(c)\s(b)" into aRegex
put "?{ fun \2 \1}" into rText

on fun x, y
	return toUpper( x & space & y)
end fun

if sunnYreplace( string, aRegex, rText, resultText) then
	put resultText # --> a B C d
end if

sunnYextract( aText, aRegex, replaceText, resultText)

returns        true if any match, false otherwise
aText          the input text
aRegex         the regular expression
replaceText    will replace the matching part of aText
resultText     the new string with only matched strings

Example:

put "a1 b2 33 c4 55 d" into string
put "(\b\d+\b)" into aRegex
put " \1." into rText

if sunnYextract( string, aRegex, rText, resultText) then
	put resultText # --> 33. 55.
end if

smartReplacementText

Back references

If you have one or more captured groups in your regex, you can refer to them with the \i syntax. i being the number of the capture group. \0 is for group 0 which correspond to the whole match.

Special characters

\t will be replaced by a tab
\n will be replaced by a return

Calling LC handlers

on Handler x
	return x*2
end Handler

# Process any matched string this way:

# " ?{ Handler \1 } "
# \1 will be replaced by the matched string of group 1.
# See the tutorial for more.

Calling LC functions

function f1 x
	return x*2
end f1

# Process any matched string this way:

# " ?{ f1( \1) } "
# \1 will be replaced by the matched string of group 1.
# See the tutorial for more.

sunnYrexError( err)

# Here is the best way to handle results
# of all sunnYrex functions
if sunnY....( ...) then
    # We found some match, proceed.
else if sunnYrexError( err) then
    answer "ERROR: " & err
else
    answer "No match!"
end if

WildCard or Regex mode

Users can choose between Wildcard or Regex mode. In wildcard mode, replace all the regex from the functions above with a wildcard pattern. WildCard patterns recognize two meta-character: * for 0 or more characters and ? for one. As captured groups are also available, you need to escape any literal ( ) as well as * or ?.
# default mode
sunnYrexSetMode "regex"
# grep-like feature ( line context)
sunnYrexSetMode "wildcard-lines"
# sed-like feature ( * and ? can match \n )
sunnYrexSetMode "wildcard-block"
# check which mode is active
put "MODE is " & sunnYrexMode()

sunnYdumpArray( Array, mode , sep1, sep2)

Displays an array in a human readable form. There are few options to change the style of presentation; Pretty mode is the default. For compact results use the "Plain" mode for a CSV-like layout.
sunnYdumpArray( R2)
sunnYdumpArray( R2, "pretty", " . ", " : ")
sunnYdumpArray( R2, "plain")
sunnYdumpArray( R2, "plain", comma, ";")
sunnYdumpArray( R2, "plainV", " ", "||")
sunnYdumpArray( R2, "plainVerbose", " ", "||")