Multi Line Triggers, Regex hell.

Posted by Shigs on Wed 06 Aug 2008 12:34 AM — 7 posts, 29,564 views.

#0
I have multi-line strings.


Quote:
Tarmeh tells you his name. You feel some enlightenment and gain 1700
experience from the encounter.


Quote:

Tarmeh tells you his name and a lot about himself. You feel enlightened
by the information and gain 3850 experience from the encounter.
Quote:


How in my trigger do I account for, Tarmeh, isn't a constant, and how do I aqquire the number of experince points into a variable?
Netherlands #1
That depends. Is the mud manually inserting newlines after a word, depending on the line length? (E.g. is the newline always after the same word or not?).

If it is always after the same word, just use \n to match the newline at the right spot. If it varies, you'll want to use \n? at every possible spot where the mud could insert a newline.

If your mud supports a soft-wrapping modus (letting your client do the wrapping) rather than wrapping lines itself, it would be easiest to turn that on and totally forget about matching on newlines.
Australia Forum Administrator #2
This might do it. Your basic problem is that the line break might appear after every word, so I changed a space to \s+ which is "one or more whitespace". A newline counts as whitespace, so this allows for newlines after the words.

I also made the variable parts ("some enlightenment" etc.) to just .* which would match anything.


<triggers>
  <trigger
   enabled="y"
   lines_to_match="2"
   match="(?s)^(\w+)\s+tells\s+you\s+his\s+name.*\.\s+You\s+feel\s+.+\s+and\s+gain\s+(\d+)\s+experience\s+from\s+the\s+encounter\.\Z"
   multi_line="y"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>Matched on: %0
%%1 = %1
%%2 = %2</send>
  </trigger>
</triggers>



Running this on your test data gave this (output from trigger in bold):


Tarmeh tells you his name. You feel some enlightenment and gain 1700
experience from the encounter.
Matched on: Tarmeh tells you his name. You feel some enlightenment and gain 1700
experience from the encounter.
%1 = Tarmeh
%2 = 1700

Tarmeh tells you his name and a lot about himself. You feel enlightened
by the information and gain 3850 experience from the encounter.
Matched on: Tarmeh tells you his name and a lot about himself. You feel enlightened
by the information and gain 3850 experience from the encounter.
%1 = Tarmeh
%2 = 3850
Amended on Wed 06 Aug 2008 01:11 AM by Nick Gammon
#3
Yet again Nick, You are a Legend.
#4
How would,. I check for HIS or HER?


I've tried his|her and (his|her) and gotten nothing but errors, the trigger seems to fire, but the second parameter, returns a null value,
USA #5
If you add a new capture group by using parentheses, then you will have to change the %2 to be a %3 to reflect the fact that it is now the third capture group.
#6
You can make the group a non-capture group by using (?:his|her) instead of just (his|her). That will prevent it from changing your %1 value.