2

I am making an in-game script editor, and its almost complete. Aside from needing to script in key-binds and such, I need to fix some problems with the Syntax Highlighter. Here is how the editor looks so far:
(UI has had no work done to it, ignore it.)

Picture of editor in game

As you can clearly see, there are some bugs to work out, such as some parenthesis refusing to highlight. The bigger problem resides in my pattern for findings strings though:
(([^\\]*["\']).*%2)
This pattern causes this:
Items between two strings become highlighted
I took a break from working on this a few weeks ago, and I don't really remember as well on how patterns work. I have tried modifying my existing pattern to prevent text between strings from being included with match, but it either selects EVERYTHING, or doesn't find any strings.
I need to make sure it doesn't find \' or \" to be an indicator for a string either.

1
  • Please provide more details, are you looking for a regex match? Commented May 20, 2020 at 3:00

2 Answers 2

0

Your pattern is a bit complicated. You can simply match balanced pairs of " using the pattern %b""

local myString = 'local a = {"one", "two", "tree"}'

for str in myString:gmatch('%b""') do print(str) end

prints

"one"
"two"
"tree"

See https://www.lua.org/manual/5.3/manual.html#6.4.1

%bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.

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

1 Comment

This doesn't solve the problem. The OP says: I need to make sure it doesn't find \' or \" to be an indicator for a string either.
0

Your issue is that you are using a greedy match. This will purposely find the longest matching string. Instead you should use a lazy match. This will allow you to match just the first string. Lazy matches use the - symbol instead of the +

So now you can write simple pattern for each variant of the string

> print(([["foo"]]):match("\"[^\"]-\""))
"foo"
> print(([["foo"  "bar"]]):match("\"[^\"]-\""))
"foo"
>

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.