RegExp help needed - tricky

Posted by Ked on Fri 13 Sep 2002 07:16 AM — 2 posts, 7,834 views.

Russia #0
I have a problem that I appear to be unable to solve myself and would greatly appreciate any help with.

Need to match a string, which consists of a range of alphameric characters, while each of those characters (including whitespaces) may be randomly substituted for with a *. For example the line:

A quick brown fox

May be represented as:

A*qui*k *rown fox

or:

**quick*br*wn *ox (or even *****************)!

Now, I've tried doing it through the use of regular expressions (in the form of [A|\*][[ ]|\*] etc.), but I just can't seem to make it function as my regexp matches on any string with a capital 'A' in it no matter how I tweak it around.
Australia Forum Administrator #1
You need to use round brackets, not square ones, like this:


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="((?:A|\*)(?: |\*)(?:q|\*)(?:u|\*)(?:i|\*)(?:c|\*)(?:k|\*)(?: |\*)(?:b|\*)(?:r|\*)(?:o|\*)(?:w|\*)(?:n|\*)(?: |\*)(?:f|\*)(?:o|\*)(?:x|\*))"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>I got &quot;%1&quot;</send>
  </trigger>
</triggers>



The "?:" is needed otherwise you get "too many subexpressions". The "or" operator "|" is used inside round brackets (eg. (cat|dog) ) not square brackets.

What the above does is match on the sentence (A quick brown fox) where any character can be replaced by an asterisk. If you had to do a longer one you could probably write a quick script to whip up the regular expression for you.

It returns it in wildcard 1 (which is what the extra round brackets at the start and end are for) which my example displays in the output window.

You can copy the above and paste into the triggers window.

If you want to match on the exact line (ie. only that sentence on the line) precede the regular expression with ^ and follow by $.

eg. ^ <-- whatever it was --> $