How do I capture part of a word?

Posted by Karl_Newbie on Sat 31 Jul 2004 08:02 PM — 4 posts, 16,135 views.

USA #0
note: I figured out an alternative to solution.

This should be an easy one. On the line I see something like this.

Bernshire (5h2g3i23) men: 120 req: 1200 current: 0 (garrison)


My trigger below is intended to match that line and evaluate if the 'req: *' is greater than the 'current: *'.

If the 'req # is higher, it sends a command to the mud using some of the other parts of that line.
The command seems to send fine.
The problem is with the '(5h2g3i23)' The command being sent to the mud can only use what's inside the parenthesis, but my trigger matches all of it, including the parenthesis.


I tried using escape chars as the alternative trigger below, but then the line does not match at all.


"(@aland) \((.*?)\) men: (.*) req: (.*?) current: (.*?) (.*?)"


Okay edit: I got the trigger to work well on my own. Without regular expressions. I'm still not sure why the regular expressions didn't work.
"@aland (*) * men: * req: * current: * *"





match="(@aland) \((.*?)\) men: (.*) req: (.*?) current: (.*?) (.*?)"
Below is what I have.
The problem is it sends
'resupply (5h2g3i23) 1200'

When I just want 'resupply 5h2g3i23 12000 (those spaces don't seem to bother the mud)

-------------------------------------------------------
<triggers>
<trigger
custom_colour="2"
enabled="y"
expand_variables="y"
group="payland"
ignore_case="y"
match="(@aland) (.*?) men: (.*?) req: (.*?) current: (.*?) (.*?)"
regexp="y"
repeat="y"
send_to="12"
>
<send>if %4 &gt; %5 then
Send &quot;resupply %2 %4&quot;
world.note &quot;supplying now debug W1= %1 W2= %2 W3= %3 W4= %4 W5 = %5 W6= %6 W7= %7&quot;
else
world.note &quot;all required supplies are present Debug W1= %1 W2= %2 W3= %3 W4= %4 W5 = %5 W6= %6 W7= %7&quot;
end if

</send>
</trigger>
</triggers>
Amended on Sat 31 Jul 2004 10:17 PM by Karl_Newbie
Australia Forum Administrator #1
Quote:

Bernshire (5h2g3i23) men: 120 req: 1200 current: 0 (garrison)

...

I tried using escape chars as the alternative trigger below, but then the line does not match at all.


"(@aland) \((.*?)\) men: (.*) req: (.*?) current: (.*?) (.*?)"


You didn't escape enough, there are two lots of brackets in the line you are trying to match. Also, I wouldn't bother putting the name (@aland) in brackets, you don't need that back, after all it is already in a variable. Try this:


@aland \((.*?)\) men: (.*) req: (.*?) current: (.*?) \((.*?)\)

Amended on Sat 31 Jul 2004 11:33 PM by Nick Gammon
Australia Forum Administrator #2
If you are going to use regular expressions you may as well do it properly. For one thing, you are matching numbers so use \d instead of . (which matches any letters) and use + instead of *. + matches one or more, whereas * matches zero or more. Zero numbers won't be valid (by zero numbers I mean no numbers, not the number zero). Thus it might look like this:


@aland \((.*)\) men: (\d+) req: (\d+) current: (\d+) \((.*)\)

Australia Forum Administrator #3
Then if you want to get fancy, use named wildcards, like this:


@aland \((?P<who>.*)\) men: (?P<men>\d+) req: (?P<req>\d+) current: (?P<current>\d+) \(?P<garrison>(.*)\)


Then you can test using names in the send text, like this:


if %<req> > %<current> then
  send "resupply %<who> %<req>"
end if


That makes it easier to read, without having to count which wildcard is which.