|
thrace Newbie
Joined: 26 Mar 2008 Posts: 5
|
Posted: 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. |
|
|
|
chamenas Wizard
Joined: 26 Mar 2008 Posts: 1547
|
Posted: 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} |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: 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} |
|
|
|
chamenas Wizard
Joined: 26 Mar 2008 Posts: 1547
|
Posted: Wed Mar 26, 2008 8:14 pm |
#TRIGGER {* anvil ~(glowing~)} {command}
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: 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.
|
|
|
|
chamenas Wizard
Joined: 26 Mar 2008 Posts: 1547
|
Posted: Wed Mar 26, 2008 8:28 pm |
Ah, fun.
|
|
|
|
thrace Newbie
Joined: 26 Mar 2008 Posts: 5
|
Posted: 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
- don't work.
Any help please? |
|
|
|
Larkin Wizard
Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: 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} |
|
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: 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 :( |
|
|
|
thrace Newbie
Joined: 26 Mar 2008 Posts: 5
|
Posted: 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
display only "the", however i wish this command display whole string "the seventh part of magic" |
|
|
|
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: 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.
|
|
|
|
thrace Newbie
Joined: 26 Mar 2008 Posts: 5
|
Posted: 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 |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: 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.
|
|
|
|
thrace Newbie
Joined: 26 Mar 2008 Posts: 5
|
Posted: 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
is vary and can contains one or more words. And text
is constant. |
|
|
|
Larkin Wizard
Joined: 25 Mar 2003 Posts: 1113 Location: USA
|
Posted: Fri Mar 28, 2008 8:27 pm |
If you use the (*) from my example, you'll capture multiple words easily.
|
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: 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. |
|
|
|
jtown84 Novice
Joined: 09 Oct 2007 Posts: 36
|
Posted: 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 |
|
|
|
JQuilici Adept
Joined: 21 Sep 2005 Posts: 250 Location: Austin, TX
|
Posted: 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! |
|
|
|
jtown84 Novice
Joined: 09 Oct 2007 Posts: 36
|
Posted: 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 |
|
|
|
oldguy2 Wizard
Joined: 17 Jun 2006 Posts: 1201
|
Posted: 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? |
|
|
|
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: 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. |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: 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)} |
|
|
|
|
|