lua-users home
lua-l archive

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


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 |^$()%.[]*+-?| ?
I suggest that (not tested) :

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

%c represents all control characters
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 ?

-- 
<http://louve.dyndns.org> ou <http://mildred632.free.fr>
Le courrier E-mail est une correspondance privée. Merci de vous y conformer


Attachment: signature.asc
Description: OpenPGP digital signature