bratao
bratao

Reputation: 2080

Cannot use Percent (%) in a Lua pattern

I have this line in a Lua script that crash my software every time:

fmt_url_map = string.gsub( fmt_url_map, '%2F','/' )

I want to replace all occurrences of %2F occurrences in a text to /. If I remove the % , it doesn't crash.

What am I doing wrong ?

Upvotes: 10

Views: 9516

Answers (2)

Alex
Alex

Reputation: 15343

% is a special symbol in Lua patterns. It's used to represent certain sets of characters, (called character classes). For example, %a represents any letter. If you want to literally match % you need to use %%. See this section of the Lua Reference Manual for more information. I suspect you're running into problems because %F is not a character class.

Upvotes: 18

Tuomas Pelkonen
Tuomas Pelkonen

Reputation: 7831

You need to escape the '%' with another '%'

fmt_url_map = string.gsub( fmt_url_map, '%%2F','/' )

Upvotes: 6

Related Questions