lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Mildred wrote:
Philippe Lhoste wrote:
Perhaps that:

oldString = "Sale: 10% off, plus additional 5% off if 100% insured"
generatedPattern = "0%"
replace = "$"
pattern = string.gsub(generatedPattern, "%%", "%%%%")
newString = string.gsub(oldString, pattern, replace)
print(newString)

Seems to work, at least with this simplistic test...

What if the pattern contains characters like |^$()%.[]*+-?| ?

Watch your mouth! :-P

IIRC, the original message was concerned only by the percent sign (or so I understood), so I addressed only this case.

I suggest that (not tested) :

pattern = string.gsub(generatedPattern, "(%c)", "%1")
newString = string.gsub(oldString, pattern, replace)

%c represents all control characters

I think that the control characters are those below Ascii 32, not the chars listed above.

If it does not work, [^%w] must work because all escaped
non-alphanumeric character represent the character itself

Is it possible to add a parameter to switch to a plain text replacement
...? It would be very useful ...
Why should I use regular expressions when I only need plain text
replacement ?

Well, I don't disagree with you, it should be easy to add an additional, optional argument, without breaking compatibility.

--
Philippe Lhoste
--  (near) Paris -- France
--  http://Phi.Lho.free.fr
--  --  --  --  --  --  --  --  --  --  --  --  --  --