

Task

   match a string against a regular expression
   substitute part of a string using a regular expression


 
samples = #("Some string 123","Example text 123","string",\
	    "ThisString Will Not Match","A123,333,string","123451")
samples2 = #("I am a string","Me too.")
 
regex = dotnetobject "System.Text.RegularExpressions.Regex" ".*\bstring*"
regex2 = dotnetobject "System.Text.RegularExpressions.Regex" "\ba\b"
 
clearlistener()
 
format "Pattern is : %\n" (regex.toString())
 
for i in samples do
(
	if regex.ismatch(i) then
	(
		format "The string \"%\" matches the pattern\n" i
	)
	else
	(
		format "The string \"%\" doesn't match the pattern\n" i
	)
)
 
-- replacement
 
format "Pattern is : %\n" (regex2.toString())
 
for i in samples2 do
(
	if regex2.ismatch(i) then
	(
		local replaced = regex2.replace i "another"
		format "The string \"%\" matched the pattern, so it was replaced: \"%\"\n" i replaced
	)
	else
	(
		format "The string \"%\" does not match the pattern\n" i
	)
)
 