Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Why does my String pattern cut off numbers ?

Asked by
Velibor 20
9 years ago

For some reason my numbers are cut off in the fellow example. If someone knows what is causing this, or has a better pattern please share.

function Split(String, Pattern)
local Res       =   {}

for Query in string.gmatch(String, Pattern) do
table.insert(Res, Query)
end

return Res
end

local Table = Split('Hello world > User_-1', '[^.->]+')

for i,v in pairs(Table) do
    print(v)
end

Output :

  • Hello world
  • User_-

Should be:

  • Hello world
  • User_-1

1 answer

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Hyphens in character sets in pattern matching indicate ranges of characters, for example "[A-Z]" would mean any capital letter.

It happens to be that . precedes the numbers and is followed by < = >, causing your problem.


Move the hyphen to the end or beginning of the range and it will indicate that it is a normal hyphen and not indicating a character range.

Though it also seems just "[^>]" would be sufficient, though I'm not exactly sure what you're trying to do here because you haven't explained.


Your snippet also has an unnecessary do on line 1.

Ad

Answer this question