How to match with spaces?

Post Reply
professororange
Posts: 2
Joined: Tue Jul 28, 2015 8:59 pm

How to match with spaces?

Post by professororange » Thu Aug 20, 2015 10:13 pm

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?

tarkenton
Posts: 27
Joined: Thu Feb 07, 2013 7:33 am

Re: How to match with spaces?

Post by tarkenton » Fri Aug 21, 2015 5:05 am

^\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.

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: How to match with spaces?

Post by Nyyrazzilyss » Fri Aug 21, 2015 5:29 pm

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.

User avatar
SlySven
Posts: 919
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: How to match with spaces?

Post by SlySven » Fri Aug 21, 2015 11:32 pm

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

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests