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

Specialized String Pattern Using Periods?

Asked by 5 years ago

Hello,

I was wondering what string pattern I would use in order to loop through the string and use the periods as separators. So, a good example of this would be:

str = "workspace.IdealistDeveloper" 
specialized_str_pattern; -- Not sure what this would be yet.

for i in string.gmatch(str, specialized_str_pattern) do
    print(i)
end

The code above would print:

workspace

IdealistDeveloper

Another test run with numbers and other characters in use:

str = "workspace.CrayMonkey1212_.Head"
specialized_str_pattern; -- Not sure what this would be yet.

for i in string.gmatch(str, specialized_str_pattern) do
    print(i)
end

The code above would print:

workspace

CrayMonkey1212_

Head

Thanks in advance for any help you may provide.

Best regards,

IdealistDeveloper

0
I'd like to help, though your end-goal is a bit confusing Ziffixture 6913 — 5y
0
You're requesting a line separator? Ziffixture 6913 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

The answer here is a fairly simple one, and there are really two ways of doing this ,dependent on what you plan on doing. If the code you have has no special characters besides the period, you can do [^%p]+ which gets non punctuation characters in a given string. However, if there are special characters , you would have to use [^%.]+, which gets non-period characters.

[^%.]+ is a special case as the period is a string pattern all to itself, being the pattern for all characters. To cancel out this characteristic, we can use the % character before it.

To shorten the code a bit, we can use the method version of the functions of the string library. Doing so is simply to shorten and make the code more simplfied, as

str:gmatch(pattern)

essentially functions the exact same as

string.gmatch(str,patter)

with that said, here is the product:

Case 1: ([^%.]+ can also be used here)

local str = "workspace.IdealistDeveloper" 
local specialized_str_pattern = "[^%p]+"

for i in str:gmatch(specialized_str_pattern) do
    print(i)--workspace, IdealistDeveloper
end

Case 2:

local str = "workspace.CrayMonkey1212_.Head"
local specialized_str_pattern  = "[^%.]+"

for i in str:gmatch(specialized_str_pattern) do
    print(i)--workspace , CrayMonkey1213_, Head
end

Another thing I would like to say is that the use of local variables is preferred for variables only used in its scope, and not a higher one, including the global scope.

This explanations was really skimming the surface here, to look at a more in depth and general explanation, go to the link below.


Reference


string patterns and "magic characters"

Hopefully this helped!

0
Ha, I was trying to answer this too. Instead I came up with a complex algorithm. I forgot the simple Logic;) Great answer anyway! Ziffixture 6913 — 5y
0
thanks, I want to see this algorithm of yours ;) theking48989987 2147 — 5y
0
Thank you so much, this helped a bunch with my game! IdealistDeveloper 234 — 5y
Ad

Answer this question