question about capturing messages with regular expressions

Posted by Gunner on Thu 23 Jul 2020 12:38 AM — 5 posts, 21,577 views.

Argentina #0
Hi.
I would like to know how to capture a message until the point in which there are multiple spaces in a row, and if after those spaces, there is more text, capture it in another wildcard.
Thanks
Australia Forum Administrator #1
This should do it:


^(.+) {2,}(.*)?$


To break that down:

  • ^ --> Assert start of line
  • (.+) --> match anything, capture it
  • <space>{2,} --> match two or more spaces (note there is a space before the brace)
  • (.*)? --> match anything, capture it (optional)
  • $ --> Assert end of line


Example trigger:


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="^(.+) {2,}(.*)?$"
   regexp="y"
   send_to="2"
   sequence="100"
  >
  <send>

wildcard 1 is: %1
wildcard 2 is: %2

</send>
  </trigger>
</triggers>


Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.
Amended on Thu 23 Jul 2020 03:29 AM by Nick Gammon
Argentina #2
OK, but don't work :)
My trigger is:
Match: ^\s*(\d+)\)\s\[(\d+)\]\s\w+\s+(.+) {2,}(.+) {2,}$

the message to capture is:

"6) [65] Consigue el Martillo del Alma                 Infierno                "


In the wildcard %3 the text is: "el martillo del alma infierno"
So I would like to capture "el martillo del alma" in the wildcard %3, and "infierno" in the wildcard %4
Amended on Thu 23 Jul 2020 12:50 PM by Fiendish
Australia Forum Administrator #3
It might have saved a day if you had mentioned your exact data in your first post.

Your problem here is that you have two places with multiple spaces, and the regexp isn't sure which one to stop at.

It might be easier to just capture the first part of the line (which is easy) and then use scripting to pull out up to the first lot of multiple spaces


<triggers>
  <trigger
   custom_colour="2"
   enabled="y"
   match="^\s*(\d+)\)\s\[(\d+)\]\s(.*)$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

print ("wildcard 1 is: %1")
print ("wildcard 2 is: %2")
print ("wildcard 3 is: %3")

first_part, second_part = string.match ("%3", "^(.-)%s%s+(.*)")

print ("first_part is", Trim (first_part))
print ("second_part is", Trim (second_part))

</send>
  </trigger>
</triggers>



The Lua pattern looks for anything (non-greedy) followed by two or more spaces, and then anything else.
USA Global Moderator #4
I don't think that's needed. Just make the ".+" nongreedy instead.

^\s*(\d+)\)\s\[(\d+)\]\s\w+\s+(.+?) {2,}(.+?) {2,}$

See https://regex101.com/r/f32tc7/1