Regular expressions
Terry Vogelaar
tvogelaar at de-mare.nl
Tue Jul 2 13:13:29 EDT 2013
Hi Jan,
This sure is helpful.
However, I need to do some thinking how I can turn this into a search-and-replace-all style action. I probably need to write a loop where I use offset to find the first "<span", then find the first "</span>", then use matchChunk to see if it matches and to put things in variables, and then change the span tags into something else temporarily to keep the offset function to find the first one over and over again, and if the last one is found, do some cleaning up to get the span tags back into place.
Doable, but way more complex than it is in BBEdit using GREP.
Thanks for the response.
With kind regards,
Terry Vogelaar
Op 2 jul. 2013, om 16:31 heeft Jan Schenkel <janschenkel at yahoo.com> het volgende geschreven:
> Hi Terry,
>
> Maybe the 'matchChunk' function can help you achieve your goal?
> From its dictionary entry:
> ####
> Syntax:
> matchChunk(string,regularExpression[,positionVarsList])
> ...
> If the regularExpression includes a pair of parentheses, the position of the substring matching the part of the regular expression inside the parentheses is placed in the variables in the positionVarsList. The number of the first character in the matching substring is placed in the first variable in the positionVarsList, and the number of the last character is placed in the second variable. Additional starting and ending positions, matching additional parenthetical expressions, are placed in additional pairs of variables in the positionVarsList. If the matchChunk function returns false, the values of the variables in the positionVarsList are not changed.
> ####
>
>
> So if matchText(...) returns true, and you've provided enough start and end match variables, you can iterate backwards over those match position pairs to insert the replacement text. Or maybe I haven't completely grasped what you were trying to accomplish - it's been a long day.
>
>
> Jan Schenkel.
>
>
> =====
> Quartam Reports & PDF Library for LiveCode
> www.quartam.com
>
>
> =====
> "As we grow older, we grow both wiser and more foolish at the same time." (La Rochefoucauld)
>
>
> ----- Original Message -----
>> From: Terry Vogelaar <tvogelaar at de-mare.nl>
>> To: How to use LiveCode <use-livecode at lists.runrev.com>
>> Cc:
>> Sent: Tuesday, July 2, 2013 1:19 PM
>> Subject: Regular expressions
>>
>> G REP saved my bacon in several occasions. And the regular expressions in LC are
>> somewhat similar to GREP, but not quite.
>>
>> In GREP I can do this:
>>
>> stringToChange: <body><span>Block of text</span></body>
>> matchExpression: <span>[^<]+</span>
>> replacementString: <div>?</div>
>> result: <body><div><span>Block of
>> text</span></div></body>
>>
>> The question-mark fills in everything that matches the matchExpression. Very
>> useful when you want to put something around the found instances.
>>
>> Or you can even take it a step further by using ( and ) in combination with
>> \1, \2 etc.:
>>
>> stringToChange: <body><span class="MakeMeVisible">Block of
>> text</span></body>
>> matchExpression: <span
>> class="([^"]+)">([^<]+)</span>
>> replacementString: <span>\1: \2</span>
>> result: <body><span>MakeMeVisible: Block of
>> text</span></body>
>>
>> The \1 fills in what it found inside the first pair of parentheses; the
>> \2 the second pair. This way you can do really powerful replacements, if you
>> know what you are doing.
>>
>> So I wonder how I do this in LC. I know I need to use Perlre and not GREP. So
>> how do I do this in Perlre?
>>
>> Terry
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription
>> preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list