Reg exp trouble!

Posted by Cyb Ercho Lin on Sat 18 Mar 2017 04:51 AM — 6 posts, 30,287 views.

Argentina #0
I am having problems to be able to do a trigger that orders me by categories an information that throws a command to me.

This is an example of what appears in a single line:


     Level of knowledge 90%   Level of domain    High     Chakra   200
              Healing ability    Gifted      Reach 5
     Inheritance 50



In this example, the first line has 3 components, the second line 2 components and the third 1 component. The order of the 3 lines is random.
I would like to use a trigger to sort these lines.

for example:

Line 1:
Level of knowledge 90%
Level of domain High
chakra 200

Line 2:
Healing ability Gifted
reach 5

Line 3:
Inheritance 50

I've tried to fix it with this:

Reg Exp:


[\s](.+?)[\s](\d+)\%[\s](.+)[\s](\d+)[ ](\d+)[\s](.+)[\s](\d+)[\s]



With this line only shows me the data of the first line that contains 3 parts, but if there are only 2 parts does not show anything and if it is only 1 part also does not show the data.

Any idea how to fix it?
Amended on Sat 18 Mar 2017 09:06 PM by Nick Gammon
Australia Forum Administrator #1
Can you please post the actual trigger?

Template:copying
For advice on how to copy aliases, timers or triggers from within MUSHclient, and paste them into a forum message, please see Copying XML.


I'm a bit confused about your regexp. A "set" is something in brackets, like [abc] which would match "a" or "b" or "c".

When you have this:


[\s]


That is only going to match a space, so the brackets aren't required. Plus it only matches one space, and in your example you have multiple spaces. To match one or more spaces you want:


\s+
Argentina #2
I made some changes after reading your suggestion in the change to \s+


The actual code:

<trigger
   enabled="y"
   expand_variables="y"
   group="Books"
   keep_evaluating="y"
   match="\s+(.*)\s+(\d+)\%\s+(.*)\s+(\d+)\%\s+(.*)\s+(\d+)\%\s+$"
   omit_from_log="y"
   omit_from_output="y"
   regexp="y"
   send_to="2"
   sequence="100"
  >
   <send>%1 %2
%3 %2
%5 %6
</send>
  </trigger>


Works fine when there are 3 elements in the line as in line 1 of the example. If it is the second line or the third, it does not work.

Use the following line to format the text.
As I said before, it works perfectly if there are 3 elements in the line, but if there are only 2 or 1 element does not work.

ColourNote ("white", "", string.format ("%%-25s     %i%s", Trim ("%1"), %2,"%"))


The result:


Level of knowledge                        90% 
Level of domain                             High 
chakra                                          200


with 2 elements:

          Healing ability    Gifted      Reach 5
Amended on Sun 19 Mar 2017 06:38 AM by Nick Gammon
Australia Forum Administrator #3
You can make part of the regexp optional. eg.


^(part one)(\s+part two)?(\s+part three)?$


This has to match "part one" but also matches "part one part two" and also "part one part two part three".

Template:regexp
Regular expressions
  • Regular expressions (as used in triggers and aliases) are documented on the Regular expression tips forum page.
  • Also see how Lua string matching patterns work, as documented on the Lua string.find page.
Argentina #4
Nick Gammon said:

You can make part of the regexp optional. eg.


^(part one)(\s+part two)?(\s+part three)?$


This has to match "part one" but also matches "part one part two" and also "part one part two part three".



Part 1: \s+(.*)\s+(\d+)\%
Part 2: \s+(.*)\s+(\d+)\%
Part 3: \s+(.*)\s+(\d+)\%\s+$


^((.*)\s+(\d+)\%)(\s+(.*)\s+(\d+)\%)?(\s+(.*)\s+(\d+)\%)?\s+$

But with this regexp not working
Australia Forum Administrator #5

Part 2: \s+(.*)\s+(\d+)\%


"Level of domain High" doesn't have a % symbol in it.