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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
thrace
Newbie


Joined: 26 Mar 2008
Posts: 5

PostPosted: Wed Mar 26, 2008 8:01 pm   

[Newbie question] Pattern matching
 
Hi,

for example CMUD display this lines

Code:
You are carrying:
the seventh part of magic anvil (glowing)


How i put in some variable string "the seventh part of magic"?

Thanks for answers.
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Wed Mar 26, 2008 8:05 pm   
 
You want it to trigger whenever "the seventh part of magic" comes up?

#TRIGGER {the seventh part of magic} {command}
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Wed Mar 26, 2008 8:09 pm   
 
I think he means that "the seventh part of magic" can vary. So:

#trig {anvil ~(glowing~)} {command}

would do. Or you could use:

#trig {[%w ] anvil ~(glowing~)} {command}

or

#regex {[\w ]+? anvil \(glowing\)} {command}
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Wed Mar 26, 2008 8:14 pm   
 
#TRIGGER {* anvil ~(glowing~)} {command}
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Wed Mar 26, 2008 8:18 pm   
 
Yes, that'll also work, but it'll be very slow. * at the beginning or end of a pattern takes ages to match (there was a long discussion about that in another thread), so you should only use it if you absolutely have to.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
chamenas
Wizard


Joined: 26 Mar 2008
Posts: 1547

PostPosted: Wed Mar 26, 2008 8:28 pm   
 
Ah, fun.
_________________
Listen to my Guitar - If you like it, listen to more
Reply with quote
thrace
Newbie


Joined: 26 Mar 2008
Posts: 5

PostPosted: Thu Mar 27, 2008 1:18 pm   
 
Fang Xianfu wrote:
I think he means that "the seventh part of magic" can vary. So:

#trig {anvil ~(glowing~)} {command}

would do. Or you could use:

#trig {[%w ] anvil ~(glowing~)} {command}

or

#regex {[\w ]+? anvil \(glowing\)} {command}


Thanks for help, but i really dumb;D

i use u trigger
Code:
#trig {[%w ] anvil ~(glowing~)} {command}

but i can't assign string "the seventh part of magic" to variable. I tried command
Code:
#va test %1
- don't work.
Any help please?
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Thu Mar 27, 2008 1:27 pm   
 
If you're trying to capture the first part, you need something more like:
Code:
#TRIGGER {(*) anvil ~(glowing~)} {#VAR test %1}
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Mar 27, 2008 1:28 pm   
 
Put brackets around what you want to capture. So:

([%w ]) anvil ~(glowing~)

Edit: Gah! You're doing it deliberately now :(
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
thrace
Newbie


Joined: 26 Mar 2008
Posts: 5

PostPosted: Thu Mar 27, 2008 9:01 pm   
 
Fang Xianfu wrote:
Put brackets around what you want to capture. So:

([%w ]) anvil ~(glowing~)

Edit: Gah! You're doing it deliberately now :(


Yes, its work, but ;D
when i use trigger
Code:
#trig {([%w ]) anvil ~(glowing~)} {#va test %1}
then command
Code:
#echo @test
display only "the", however i wish this command display whole string "the seventh part of magic"
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Thu Mar 27, 2008 9:08 pm   
 
I have a question...is it always an "anvil" in that line? Are you trying to capture the level of magic on the anvil or something? I'm just trying to figure out why you are capturing "the seventh part of magic" instead of "the seventh part of magic anvil" since the anvil is the actual item.
Reply with quote
thrace
Newbie


Joined: 26 Mar 2008
Posts: 5

PostPosted: Fri Mar 28, 2008 8:23 am   
 
oldguy2 wrote:
I have a question...is it always an "anvil" in that line? Are you trying to capture the level of magic on the anvil or something? I'm just trying to figure out why you are capturing "the seventh part of magic" instead of "the seventh part of magic anvil" since the anvil is the actual item.


Nope,

this was only example, in fact i want capture string, which starting at the beginning of the line, and ends before <any word>

e.g.

i received from mud line
Code:
a dangerous flower is dead!  R.I.P.


and i wish capture string "a dangerous flower".
Or

Code:
Memory mob gossips Great reward is waiting for anyone who kills me!!


i want capture "Memory mob", etc
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Fri Mar 28, 2008 2:28 pm   
 
I'm afraid you'll have to explain a bit. Those are completely different lines. What exactly are you trying to do? Are you trying to learn the general method of constructing a capture within a trigger? Or are you trying to make some specific triggers for these situations? The three example lines you've given would need separate triggers.
Reply with quote
thrace
Newbie


Joined: 26 Mar 2008
Posts: 5

PostPosted: Fri Mar 28, 2008 7:53 pm   
 
Rahab wrote:
I'm afraid you'll have to explain a bit. Those are completely different lines. What exactly are you trying to do? Are you trying to learn the general method of constructing a capture within a trigger? Or are you trying to make some specific triggers for these situations? The three example lines you've given would need separate triggers.


Okey, i want make trigger, which capture part of line. I try explain u on previous example. I received from mud this line
Code:
a dangerous flower is dead!  R.I.P.
.
Text
Code:
a dangerous flower
is vary and can contains one or more words. And text
Code:
is dead!  R.I.P.
is constant.
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Fri Mar 28, 2008 8:27 pm   
 
If you use the (*) from my example, you'll capture multiple words easily.
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Fri Mar 28, 2008 8:35 pm   
 
Right. For that one, the following would store the name (e.g. a dangerous flower) in the variable @test:

#trig {(*) is dead! R.I.P.} {#va test %1}

Unfortunately, this trigger has an initial *, since I imagine the number of words you need to capture can vary. That means it will be a slow trigger, comparatively.

There is an alternative which avoids the initial *:

#trig {" is dead! R.I.P."} {#call %match(%line, "* is dead! R.I.P.", $name); #va test $name}

This version only does the greedy matching on lines which we know will match.
Reply with quote
jtown84
Novice


Joined: 09 Oct 2007
Posts: 36

PostPosted: Sat Mar 29, 2008 6:58 am   
 
Quote:

#trig {" is dead! R.I.P."} {#call %match(%line, "* is dead! R.I.P.", $name); #va test $name}

If that works, i've got soo many triggers to change.. :(
_________________
Aardwolf
Reply with quote
JQuilici
Adept


Joined: 21 Sep 2005
Posts: 250
Location: Austin, TX

PostPosted: Sat Mar 29, 2008 6:20 pm   
 
Both versions will work. It's just that the simpler version ('#trig {(*) is dead! R.I.P.} {#va test %1}') will be a LOT slower, since the pattern-matching engine has to do a lot of work to decide whether to accept or reject a line.

Might not matter with only a couple of triggers, but if you start getting dozens to hundreds (and some of us DO have that many triggers), you'll begin to see noticeable or even crippling lag as each line is received from your MUD. Rahab is just trying to spare you some of that pain later.
_________________
Come visit Mozart Mud...and tell an imm that Aerith sent you!
Reply with quote
jtown84
Novice


Joined: 09 Oct 2007
Posts: 36

PostPosted: Sat Mar 29, 2008 7:30 pm   
 
Yeah, I have a ton of triggers that i thought HAD to start with * or (*) for matching that first word or so..
Seeing how that it isnt 100% required.. It would speed things up, I'm all for it, I hate seeing my client lag.
_________________
Aardwolf
Reply with quote
oldguy2
Wizard


Joined: 17 Jun 2006
Posts: 1201

PostPosted: Sun Mar 30, 2008 4:20 pm   
 
This is what I use instead of a greedy star at the beginning of a line in Regex. I'm sure someone will say it is wrong, but it works just fine.

Code:
(?>\w+,?\s?)+?is dead!\s+R\.I\.P\.$


That will match:

a dangerous flower is dead! R.I.P.

Bob, the warrior from the mountains is dead! R.I.P.

Bob is dead! R.I.P.

It appeared to me there is more than one space between dead and R.I.P. If there isn't it would be "\s" instead of "\s+" there.

Also if you know the beginning of the line would never contain a comma you could take out the ",?" part.

The alternative Rahab suggested is just capturing the length of the matched pattern to the variable test?
Reply with quote
Anaristos
Sorcerer


Joined: 17 Jul 2007
Posts: 821
Location: California

PostPosted: Sun Mar 30, 2008 6:07 pm   
 
Quote:

It appeared to me there is more than one space between dead and R.I.P. If there isn't it would be "\s" instead of "\s+" there.

Using \s* would work in that situation regardless of the number of blanks.
_________________
Sic itur ad astra.
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2320

PostPosted: Sun Mar 30, 2008 6:56 pm   
 
Oops, just noticed an error in my second suggested code. I forgot parentheses in the %match. And I could have used the @test within the %match instead of the local variable:

#trig {" is dead! R.I.P."} {#call %match(%line, "(*) is dead! R.I.P.", @test)}
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD 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