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

What is the string pattern for a space?

Asked by 10 years ago

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!

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. jobro13 980 — 10y

1 answer

Log in to vote
2
Answered by
jobro13 980 Moderation Voter
10 years ago

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
--]]
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 DiamondBladee 135 — 10y
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 :) jobro13 980 — 10y
Ad

Answer this question