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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd probably have suggested '%["(%d+)"%]' instead to avoid the confusion of needing to escape the embedded double quotes.
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.

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.