In Lua I have the following text and want for format it:
Incoming:
<UNIT STYLE="BOLD">Example</UNIT>
<UNIT STYLE="HANDLE">Example 2</UNIT>
<UNIT STYLE="THR">Example 3</UNIT>
Expected result:
<BOLD>Example</BOLD>
<UNIT STYLE="HANDLE">Example 2</UNIT>
<UNIT STYLE="THR">Example 3</UNIT>
The values can be different (e.g. Example, This is a Text...).
How to this? Do I have to use gsub
with a pattern?
Yes, gsub
is the way to go:
s=[[
<UNIT STYLE="BOLD">Example</UNIT>
<UNIT STYLE="HANDLE">Example 2</UNIT>
<UNIT STYLE="THR">Example 3</UNIT>
]]
print(s:gsub('<UNIT STYLE="BOLD">(.-)</UNIT>','<BOLD>%1</BOLD>'))
Try also this:
print(s:gsub('<UNIT STYLE="(.-)">(.-)</UNIT>','<%1>%2<%1>'))