Frequently, I am extracting the text data between two different tags or delimiters of varying length.
I like to use a Regex function, but even though trying various models published, I failed to get it working. Maybe someone has an idea to use the function below as a Regex function?
My LiveCode function looks like this:
Usage:
... Extract a number enclosed in parenthesis "(" and ")"
... Extracting text between a start tag, for example "---", and an ending tag "---/"
... Defining one's own tags to extract text between such tags
... Extract tags from HTML or any other tagged source code
Note:
This function only returns the FIRST instance in a string. To go through a whole document, the function needs to be extended.
Code: Select all
// Example 1: Using "(" as the beginning tag and ")" as the ending tag
put "James hit the ball in the (23)rd soccer game in Milano." into tText
put filterTags (tText, "(,)" )
Code: Select all
// Example 2: Using beginning tag "---" and "---/" as ending tag
put "James played the ball in the at the ---23rd---/ soccer game." into tText
put filterTags (tText, "---,---/" )
Code: Select all
function filterTags pString,pDels
## Extract text that is between two different delimiters/tags
if pString = "?" then
return "filterTags ( pString, pDels ). "& \
"Param 'pDels': One or two delimiters ('tags','separators') "& \
"as comma separated items."
end if
local a,b
if the number of items of pDels is 2 then
put item 1 of pDels into pDel1
put item 2 of pDels into pDel2
else
return empty
end if
if pDel1 is pDel2 then return empty -- requires two tags that are different
put offset( pDel1 , pString ) + length( pDel1 ) into a
put offset( pDel2 , pString ) - 1 into b
if a > 0 AND b > a then
return char a to b of pString
else
return empty
end if
end filterTags
s
Regards, Roland (golife)