So I am using lua and splitting a string by spaces to write a sort of sub-language. And I am trying to have it not split anything inside parenthesis, I am already at the stage where I can detect whether there is parenthesis. But I want to reverse the gmatching of the string inside the parenthesis as I want to preserve the string contained within.

```
local function split(strng)
    local __s={}
    local all_included={}
    local flag_table={}
    local uncompiled={}
    local flagged=false
    local flagnum=0

    local c=0
    for i in string.gmatch(strng,'%S+') do
        c=c+1
        table.insert(all_included,i)
        if(flagged==false)then
            if(string.find(i,'%('or'%['or'%{'))then
                flagged=true
                flag_table[tostring(c)]=1
                table.insert(uncompiled,i)
                print'flagged'
            else 
                table.insert(__s,i)
            end
        elseif(flagged==true)then
            table.insert(uncompiled,i)
            if(string.find(i,'%)' or '%]' or '%}'))then
                flagged=false
                local __=''
                for i=1,#uncompiled do
                    __=__ .. uncompiled[i]
                end
                table.insert(__s,__)
                print'unflagged'
            end
        end
    end

    return __s;
end

```

This is my splitting code
['https://Stackoverflow.com/questions/55231329', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7307551/']
I would just not use `gmatch` for this at all.

```
local input = " this is a string (containg some (well, many) annoying) parentheses and should be split. The string contains  double   spaces. What should be done? And what about trailing spaces? "

local pos = 1
local words = {}
local last_start = pos
while pos <= #input do
    local char = string.byte(input, pos)

    if char == string.byte(" ") then
        table.insert(words, string.sub(input, last_start, pos - 1))
        last_start = pos + 1
    elseif char == string.byte("(") then
        local depth = 1
        while depth ~= 0 and pos + 1 < #input do
            local char = string.byte(input, pos + 1)
            if char == string.byte(")") then
                depth = depth - 1
            elseif char == string.byte("(") then
                depth = depth + 1
            end
            pos = pos + 1
        end
    end
    pos = pos + 1
end
table.insert(words, string.sub(input, last_start))

for k, v in pairs(words) do
    print(k, "'" .. v .. "'")
end

```

Output:

```
1   ''
2   'this'
3   'is'
4   'a'
5   'string'
6   '(containg some (well, many) annoying)'
7   'parentheses'
8   'and'
9   'should'
10  'be'
11  'split.'
12  'The'
13  'string'
14  'contains'
15  ''
16  'double'
17  ''
18  ''
19  'spaces.'
20  'What'
21  'should'
22  'be'
23  'done?'
24  'And'
25  'what'
26  'about'
27  'trailing'
28  'spaces?'
29  ''

```

Thinking about trailing spaces and other such problems is left as an exercise for the reader. I tried to highlight some of the possible problems with the example that I used. Also, I only looked at one kind of parenthesis since I do not want to think how `this (string} should be ]parsed`.

Oh and if nested parenthesis are not a concerned: Most of the code above can be replaced with a call to `string.find(input, ")", pos, true)` to find the closing parenthesis.

Please note that you cannot `or` or `and` patterns as attempted in your code.

`"%(" or "%["` equals `"%("`

Lua will interpret that expression left to right. `"%(` is a true value Lua will reduce the expression to `"%("`, which logically is the same as the full expression.

So `string.find(i,'%('or'%['or'%{')` will only find `(`'s in `i`.
As a similar but slightly different approach to Uli's answer, I would first split by parentheses. Then you can split the the ***odd***-numbered fields on whitespace:

```
split = require("split") -- https://luarocks.org/modules/telemachus/split

split__by_parentheses = function(input)
    local fields = {}
    local level = 0
    local field = ""

    for i = 1, #input do
        local char = input:sub(i, i)

        if char == "(" then
            if level == 0 then 
                -- add non-parenthesized field to list
                fields[#fields+1] = field 
                field = ""
            end
            level = level + 1
        end

        field = field .. char

        if char == ")" then
            level = level - 1
            assert(level >= 0, 'Mismatched parentheses')
            if level == 0 then 
                -- add parenthesized field to list
                fields[#fields+1] = field 
                field = ""
            end
        end
    end

    assert(level == 0, 'Mismatched parentheses')
    fields[#fields+1] = field
    return fields
end

input = " this is a string (containg some (well, many) annoying) parentheses and should be split. The string contains  double   spaces. What should be done? And what about trailing spaces? "

fields = split__by_parentheses(input)

for i, field in ipairs(fields) do
    print(("%d\t'%s'"):format(i, field))
    if i % 2 == 1 then
        for j, word in ipairs(split.split(field)) do
            print(("\t%d\t%s"):format(j, word))
        end
    end
end

```

outputs 

```
1       ' this is a string '
        1
        2       this
        3       is
        4       a
        5       string
        6
2       '(containg some (well, many) annoying)'
3       ' parentheses and should be split. The string contains  double   spaces. What should be done? And what about trailing spaces? '
        1
        2       parentheses
        3       and
        4       should
        5       be
        6       split.
        7       The
        8       string
        9       contains
        10      double
        11      spaces.
        12      What
        13      should
        14      be
        15      done?
        16      And
        17      what
        18      about
        19      trailing
        20      spaces?
        21

```