Page 1 of 1

How to match with spaces?

Posted: Thu Aug 20, 2015 10:13 pm
by professororange
I'm trying to make a script where my character teleports when other people give the command, but I'm having a difficult time matching spaces.

^\w+ says 'tele to (\w+)'$ is what I have, and I want:

send("c c t " .. matches[2])

Unfortunately, that will detect and match with one word locations like "chapel" but not two or more. So, how do I fix this so that when someone says "tele to gates of hell" I don't just ignore it?

Re: How to match with spaces?

Posted: Fri Aug 21, 2015 5:05 am
by tarkenton
^\w+ says 'tele to (.*)'$

https://regex101.com is an awesome tool that helps show you what is matching with your statements, and gives a pretty good reason as to why.

What the regex above does is it captures any number of words/characters/whatevers after to and before the '. If there's better punctuation in there somewhere, you might have to modify the line slightly, but from your example given it should work.

Re: How to match with spaces?

Posted: Fri Aug 21, 2015 5:29 pm
by Nyyrazzilyss
professororange wrote:^\w+ says 'tele to (\w+)'$ is what I have, and I want:
In particular, the capture portion of your regex: (\w+)

+ = 1 or more of previous expression
\w = capture of [a-zA-Z_0-9]

None of that includes a space. As mentioned previously (.+) would work (i'd suggestion not .* since * will match 0 or more). If you wanted to be a bit stricter, use ([a-zA-Z_ 0-9]+), however, keep in mind that's not going to match hyphens or apostrophes, etc.

Re: How to match with spaces?

Posted: Fri Aug 21, 2015 11:32 pm
by SlySven
And be careful when setting up things that respond to what others do that an enemy cannot trigger such a thing automatically - if Player Killing is a feature of the MUD you play...! :o