[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[StrongED] StrongED search and replace
I hope fellow StrongED lovers will not find this confession
a betrayal. When it comes to searching and replacing in a text I
do not use StrongED's facilities, but Lua's, by dragging in a
script made to order for the problem in hand. That is because
I am more experienced with Lua's pattern matching facilities
(both standard and Lpeg) than with StrongED's. The facilities
that StrongED offers for S & R are quite powerful, but seem
to me somewhat ad hoc. As many have remarked, finding the pattern
that does the job you want is often quite a teaser, whatever system
you use; so exploiting any experience you may have gained with
one system is often easier than learning a new system.
A comparison of possibilities:
Lpeg: (+) You can do anything that standard Lua's or StrongED's
R and S systems can do. Full power of parser-expression grammars (peg).
(-) Too powerful for small jobs. Wordy. Debugging is difficult.
Lua: (+) Easy for data capture. Syntax more orthodox.
(-) Limited to patterns recognizable without backtracking.
StrongED: (+) List of found.
(-) Yet another syntax. Ad hoc facilities.
I used to use AWK with StrongED. I have no experience of using Perl,
Python or sed, which could each use their own S & R. Talk about
embarras de richesse!
Here is a little S & R challenge by way of an example: replace all top
level matching round brackets in each line by square ones. Round brackets
occurring inside matching round brackets (i.e. at lower levels) should
not be changed.
Here is the Lua script:
#!lua
local inpat, outpat = "(%b())", "[%s]"
local change = \ (s) => outpat:format (s:sub (2,-2)) end
for line in io.lines (arg[1]) do
print ((line:gsub (inpat, change))) -- note double parentheses
end
Now it would be easy with StrongED S & R to change all opening round brackets
to square ones and all closing round brackets to square ones, but that is not
the challenge. I do not know whether StrongED S & R can meet the challenge
but I suspect not.
Because the use of scripting with StrongED is so powerful, quite a large chunk
of what is already built into StrongED becomes superfluous.
--
Gavin Wraith (gavin@xxxxxxxxxxxxxxx)
Home page: http://www.wra1th.plus.com/
--
To unsubscribe send a mail to StrongED+unsubscribe@xxxxxxxxxxx
List archives at http://diy.Torrens.org/RO/StrongED/index.html