0

Simple problem...I need to turn this RegEx pattern...

"\[\"([0-9]+)\"\]"

into a Lua pattern.

I am doing this to replace a bunch ["X"] lines with just [X] in a string where X is any number from -∞ or +∞...so that's about the only limitation. I need to port this to Lua patterns so I can use it in String.gsub.

Find: "\[\"([0-9]+)\"\]"

Also, how do I just remove the " " around the number? I need a pattern for that. If someone could help me out, I would appreciate it.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
G.T.D.
  • 386
  • 1
  • 5
  • 21

1 Answers1

2

You could try like this.

> f = "foo [\"12\"] bar"
> x = string.gsub(f, "%[\"(%d+)\"%]", "[%1]")
> print(f)
foo ["12"] bar
> print(x)
foo [12] bar

\d which matches any digits would be represented as %d in lua.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • 3
    I'd probably have suggested `'%["(%d+)"%]'` instead to avoid the confusion of needing to escape the embedded double quotes. – Etan Reisner Dec 02 '14 at 00:14
  • Thanks, that worked perfectly. As to Etan's suggestion, I see the reasoning, but I like " better and the escaping is not a huge problem for me. :) Thanks guys. – G.T.D. Dec 02 '14 at 00:22