2

i am looking to write a function in Lua that replaces all occurrences of one string with another, e.g.:

function string.replace(s, oldValue, newValue)
   return string.gsub(s, oldValue, newValue);
end;

What i need (unless Lua already has a string replace function) is a function to escape Lua regular expressionpattern strings (unless Lua already has an EscapeRegularExpressionPattern function)

i've tried to start writing the proper string.replace function:

local function EscapeRegularExpression(pattern)
    -- "." ==> "%."
    local s = string.gsub(pattern, "%." "%%%.");

    return s;
end;

function string.replace(s, oldValue, newValue)
    oldValue = EscapeRegularExpression(oldValue);
    newValue = EscapeRegularExpression(newValue);

    return string.gsub(s, oldValue, newValue);
end;

But i cannot easily think of all the Lua regular expressionpattern keywords that need to be escaped.

Bonus Example

Another example that needs a fix might be:

//Remove any locale thousands separator:
s = string.gsub(s, Locale.Thousand, "");

//Replace any locale decimal marks with a period
s = string.gsub(s, Locale.Decimal, "%.");
4
  • 2
    Lua does not have regular expressions. It has patterns. They're similar, but not as powerful as actual regexes. Commented Sep 2, 2012 at 14:31
  • 2
    Lua patterns make me think that someone got a description of regular expressions in a noisy bar one night and the next day, after they'd sobered up, implemented what they could remember. They are exactly enough like normal regular expressions to trap the unwary and no more. </rant> Commented Aug 14, 2013 at 23:35
  • 2
    @Perry You didn't pass W3C test because you forgot the opening tag! Commented Jul 23, 2014 at 9:41
  • 3
    @HakanBoztepe In html, some elements are forbidden from having a closing tag (e.g. IMG). And i think in html the rant element is forbidden from having an opening tag. Commented Jul 23, 2014 at 14:00

3 Answers 3

4

I use

-- Inhibit Regular Expression magic characters ^$()%.[]*+-?)
function strPlainText(strText)
    -- Prefix every non-alphanumeric character (%W) with a % escape character, 
    -- where %% is the % escape, and %1 is original character
    return strText:gsub("(%W)","%%%1")
end -- function strPlainText
Sign up to request clarification or add additional context in comments.

1 Comment

This will also change '/' to '%/' which is not in your magic char set
1

Check out documentation on patterns (that's section 5.4.1 for Lua 5.1), most interesting would be list of magic characters:

x: (where x is not one of the magic characters ^$()%.[]*+-?) represents the character x itself.

Escape them with preceding % before using string in gsub and you're done.

To be extra sure you can set up a while loop with string.find that have convenient "plain" flag and string.sub necessary parts of string manually.

Comments

0

You can use pl.utils.escape function from Lua Penlight library.

This is fragment from sources:

function utils.escape(s)
    utils.assert_string(1,s)
    return (s:gsub('[%-%.%+%[%]%(%)%$%^%%%?%*]','%%%1'))
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.