Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is the string pattern for a space?

What is the string pattern for a space? Like, the space the spacebar types?

Is there a pattern that does JUST the spacebar?

My goal is to print each word in a sentence, but the word I'm gets split up into I and m when I try "%a+":

for w in string.gmatch("I'm filling out a job application.", "%a+") do
	print(w)
	wait()
end

I would appreciate any replys. Thanks!

1 comment

0

Note: if you would like this approach, I'd use "[%a']+". This is a set of the %a pattern + the ' character. However you would need to define every char in this pattern and thus I think my approach with the "not set" is better.

1 answer

✓ Accepted answer

I normally use a pattern trick for those things. You can also check for "not patterns".

In this case you want to match any sequence of characters which is NOT a space.

A space pattern is btw just " ". The %s pattern also matches \n (newline) and some more "white space characters"

The pattern I would use is "[^ ]+". This matches any sequence which is NOT a space. A [] pattern defines a set of characters. If this set stats with a ^ it means NOT that set. In this case we check for non-spaces.

Output:

for w in string.gmatch("I'm filling out a job application.", "[^ ]+") do
    print(w)
-- There is no need to wait in this case.
   -- wait()
end
--[[Output:
I'm
filling
out 
a 
job
application
--]]

2 comments

0

Thanks you! I never thought about the inverse. Also, I had the wait() in there because the actual string I was going to do is huge, and would crash my PC before it could finish the loop. That's not your fault though :P

0

I see! However, you still don't need to wait for every word. Every 100 / 1000s words would be good. To check this easily, create a number before the for loop. Increment this every time you match a word. Then check for (i % 100) == 0 - if that's the case , wait(1/60). Like this your pattern matching doesn't take ages :)

This discussion is preserved as it was. New answers, comments, and votes are closed.