w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Break strings into substrings based on delimiters, with empty substrings

Now that we've cleared up what the question is about, here's the issue.

Your gmatch pattern will return all of the matching substrings in the given string. However, your gmatch pattern uses "+". That means "one or more", which therefore cannot match an empty string. If it encounters a ^ character, it just skips it.

But, if you just tried :gmatch("[^^]*"), which allows empty matches, the problem is that it would effectively turn every ^ character into an empty match. Which is not what you want.

What you want is to eat the ^ at the end of a substring. But, if you try :gmatch("([^^])^"), you'll find that it won't return the last string. That's because the last string doesn't end with ^, so it isn't a valid match.

The closest you can get with gmatch is this pattern: "([^^]*)^?". This has the downside of putting an empty string at the end. However, you can just remove that easily enough, since one will always be placed there.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.