regular expressions for **

Posted by ErockMahan on Tue 25 Mar 2008 06:50 PM — 5 posts, 22,163 views.

#0
I want a trigger to activate on

Bob tells you *

but not

** Bob tells you *

Of course, it won't always be Bob, so I replace his name with *, which leaves me with

** * tells you *.

Converting it to a regular expression, I now have:

^\*\** tells you *$

But I can't get it to work like that, and yes the 'regular expression' box is checked.
Australia Forum Administrator #1
Quote:

Converting it to a regular expression, I now have:

^\*\** tells you *$


This won't work for a couple of reasons. For one thing, you wanted things that don't have the 2 asterisks, whereas this will match if you do.

Second, in a regular expression you need to use .* to match anything, so it would be more like:


^\*\*(.*) tells you (.*)$


The brackets make the wildcards get returned as %1, %2 etc.

However to solve your problem, the simple thing is to simply make the regexp match on a name. I assume names must be A-Z? So try this:


<triggers>
  <trigger
   enabled="y"
   match="^([A-Za-z]+) tells you (.*)$"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>person = %1
what s/he said = %2</send>
  </trigger>
</triggers>


What this does is make sure that the first word consists of A-Z or a-z (upper or lower case).
#2
Apologies, I'd like to borrow this thread.

How may I capture "Apple Bat Tree" in the following? Assuming there are multiple spaces between "Tree" and "(blah)" -->
Apple Bat Tree (blah)


I'm currently using:
[A-Za-z\s]+
But this captures the multiple spaces between the last word and the brackets too.

Thank you in advance.
Amended on Thu 05 Jun 2008 10:35 AM by Thalir
Australia Forum Administrator #3
To a certain extent your problem is undefined. I hope you mean two or more spaces after Tree, otherwise it is hard to know where the first part ends and the second part starts. However this works for me:


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="^(([A-Za-z]+\s)+)\s+(.*)"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>
%%1 = %1
%%2 = %2
%%3 = %3
</send>
  </trigger>
</triggers>



Testing that against:


Apple Bat Tree         testing 1 2 3


gives:


%1 = Apple Bat Tree 
%2 = Tree 
%3 = testing 1 2 3


What I have done here is grouped things.

[A-Za-z]+ gives you a word, and then we follow it by a single space: \s

That means that: ([A-Za-z]+\s)+

is one or more of those things (a word followed by one space).

Then we have: \s+(.*)

... which is another space, followed by the rest of the line.

If you can't rely on the second space, where would you break up:


every good boy deserves fruit


You need some sort of characteristic that defines the second group as being different from the first one.

Amended on Thu 05 Jun 2008 09:55 AM by Nick Gammon
#4
Thanks, that did it!