How to check if a certain area in a string is empty

This is pretty straightforward really. I want to get the text within text, but if it’s blank, then do nothing.

Here is an example of what I mean:
image


Any questions please let me know!

-- Function to extract text between "start:" and "end:"
local function getTextBetween(input)
    -- Use string.match to capture text between "start:" and "end:"
    local extractedText = string.match(input, "start:%s*(.-)%s*end:")
    
    -- Check if extracted text is not nil or empty
    if extractedText and extractedText ~= "" then
        return extractedText
    end
    
    -- Return nil if there's no valid text
    return nil
end

-- Example usage
local foo1 = "start: foo end:"
local foo2 = "start: end:" -- No text between delimiters

-- Test the function
local result1 = getTextBetween(foo1)
local result2 = getTextBetween(foo2)

-- Output results
print(result1) -- Output: "foo"
print(result2) -- Output: nil

Im not entirely sure what you’re looking for I hope this helps if not i think i may need more details.
but i want to say if i understand correctly if foo got entered In you could use it?

1 Like

Sorry for the 10-minute delay in responding. Yep, that’s exactly what I was looking for. Thank you so much!

But just a question: What if “start:” or “end:” or both were named something different, and they may change in the future at any time?

you can change the start and end in that text, If i still understand correctly.

%s*(.-)%s

these symbols aren’t that bad to learn. Basically just ways to clear white space and such. I can give you more details if you need.

I know but- actually nevermind, it’s alright, thanks though! :slight_smile:

Thanks again for helping me out with this, you’re a hero!

I know


Sure if you want, I was going to get around to learning and understanding all of them, might as well do it now, thank you.

The pattern "start:%s*(.-)%s*end:" does the following:

  • start:: Matches the literal text “start:”.
  • %s*: Matches any whitespace (optional).
  • (.-): Captures any text (lazy match to stop at the first “end:”).
  • %s*end:: Matches any whitespace (optional) and the literal “end:”.

%s* basically he will get rid of the white spaces i only say matches because thats the tech term im sure
(.-) You can imagine (.-) as a curious person opening a box ( ) and saying, "I’ll take whatever is inside ., but I’ll stop as soon as I find the end point specified in the rest of the pattern -

as silly as that might sound.

1 Like

bro really used chat gpt. shame on you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.