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 11 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+":

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

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 — 11y

1 answer

Log in to vote
2
Answered by
jobro13 980 Moderation Voter
11 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:

01for w in string.gmatch("I'm filling out a job application.", "[^ ]+") do
02    print(w)
03-- There is no need to wait in this case.
04   -- wait()
05end
06--[[Output:
07I'm
08filling
09out
10a
11job
12application
13--]]
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 — 11y
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 — 11y
Ad

Answer this question