sunnYrex API v 1.0


Author:		Thierry Douez
License:		Needs an indy or business LiveCode license
Version:		1.0
Date:		2016-08-30

sunnYmatch( string, aRegex, matchArray, N)

returns true if any match, false otherwise
string				the input text
aRegex				the regular expression
matchArray			a multidimensional array of the form
 matchArray[N][0]		char position of start of match string of group N
 matchArray[N][1]		char position of end of match string of group  N
 matchArray[N][2]		match string of group N
N				key variable of matchArray

Example:

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

if sunnYmatch( string, aRegex, A, N) 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

sunnYreplace( string, aRegex, replacementText, resultString)

returns true if any match, false otherwise
string				the input text
aRegex				the regular expression
replacementText		string which will replace the matching part of string
resultString		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( string, aRegex, replacementText, resultString)

returns true if any match, false otherwise
string				the input text
aRegex				the regular expression
replacementText		string which will replace the matching part of string
resultString		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

You can process any matched string this way:

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

Calling LC functions

function f1 x
	return x*2
end f1

You can process any matched string this way:

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