Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
Rappy
Wanderer


Joined: 15 Jul 2005
Posts: 96

PostPosted: Wed Nov 05, 2008 5:59 pm   

%regex
 
I am new to regex, but I am learning the value of it's power.

I am not sure what I am doing wrong, but the following isn't working.

#SHOW %regex("general area of The Elemental Canyon for","general area of (\w+) for",questarea,X)

I am assuming \w+ matches 1 or more words, but it's not capturing anything. If I remove the 'for' from both the pattern and the regex pattern it works, but only captures the 'The'.

-Rappy
_________________
Windows 11 Pro,
cMUD 3.34
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Wed Nov 05, 2008 6:25 pm   
 
No, \w+ matches one or more word characters. \w on its own matches any single word character - a-z and 0-9 are considered word characters in this context, so \w will match any single letter or integer between 0 and 9. The + modifier matches 1 or more of the previous character, so \w+ will match one or more characters or numbers, but it will NOT match spaces. To include spaces in the match, you want to use a character range. You do this by putting the characters you want to match inside square brackets. So [\w ] will match a single word character (a-z0-9) or space. [\w ]+ will match multiple word characters or spaces, and thus multiple words.

In short, use [\w ]+ instead.

See also this.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Rappy
Wanderer


Joined: 15 Jul 2005
Posts: 96

PostPosted: Wed Nov 05, 2008 6:41 pm   
 
Thanks, that works perfectly.

This cut my %pos code from 8 lines down to 1 %regex line.

-Rappy
_________________
Windows 11 Pro,
cMUD 3.34
Reply with quote
Rappy
Wanderer


Joined: 15 Jul 2005
Posts: 96

PostPosted: Wed Nov 05, 2008 7:41 pm   
 
Another question that's sort of on the same subject. How can I #MAPQUERY a value with ' in the query? I am capturing the above with a regex trigger and wanting to mapquery the room, but the room can contain the ' character which causes the SQL query to fail, yet when I query in the Find Room dialog in the mapper, it has no problem.

-Rappy
_________________
Windows 11 Pro,
cMUD 3.34
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Wed Nov 05, 2008 8:21 pm   
 
You have to replace the ' with quotes within the query.

For example, part of a map query I used extensively in one of the MUDs I used to play, looks like this:

Code:

$sql=%concat( "Name like '%", %replace( @RoomSearch, "'", "''"), "%' AND ", "ZoneID", " = ", %zonenum)


It is hard to spot inside the code, but basically you have to replace " ' " with " " ".
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
Reply with quote
Rappy
Wanderer


Joined: 15 Jul 2005
Posts: 96

PostPosted: Wed Nov 05, 2008 8:26 pm   
 
Interesting, replaceing a single ' with a two ' worked. Why is that?

-Rappy
_________________
Windows 11 Pro,
cMUD 3.34
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Thu Nov 06, 2008 1:11 am   
 
Because that's what you're supposed to do - Prognoi's explanation was incorrect, but his example was right. If you select the text, you'll see that the %replace function is changing each single apostrophe into two, which is what you're meant to do.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Thu Nov 06, 2008 8:45 am   
 
Indeed, I realized that later, but was sort of in a hurry when wrote it anyway.

To my defence, I've never been able to fully explain it to myself, either.

Thinking about replacing apostrophe with quote is easier to memorize (and that's how I write it in code, too), albeit wrong.
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net