Regex failing to capture one part in wildcards

Posted by Rabbi on Fri 11 Oct 2019 02:28 PM — 5 posts, 17,413 views.

#0
The text to match looks like one of these:
<938/941Hp 251/251mn 583/586mv> [0|Witch Wood] (a goblin)

<938/941Hp 251/251mn 583/586mv> [0|Witch Wood] ()


This is the regex that I'm using:
\<(\d+)\/(\d+)Hp (\d+)\/(\d+)mn (\d+)\/(\d+)mv\> [\d\|.+] \((.*)\)


Matches 1-6 are fine, wildcard 7 should be "a goblin" some of the time.
function ProcessPrompt(name, text, wildcards)
    SetVariable("currentHealth", tonumber(wildcards[1]))
    SetVariable("maxHealth", tonumber(wildcards[2]))
    SetVariable("currentMana", tonumber(wildcards[3]))
    SetVariable("maxMana", tonumber(wildcards[4]))
    SetVariable("currentMovement", tonumber(wildcards[5]))
    SetVariable("maxMovement", tonumber(wildcards[6]))
    SetVariable("target", wildcards[7])
    Tell(wildcards[7])
    SetVariable("waitingForPrompt", "false")
end

I never see the output from that "Tell", but it works and sets the variables correctly for the other wildcards.
Amended on Sat 12 Oct 2019 12:40 PM by Rabbi
USA Global Moderator #1
The pattern is busted and will not trigger on the given text. You want \[ and \] not [ and ]
Amended on Fri 11 Oct 2019 06:22 PM by Fiendish
Australia Forum Administrator #2
If you are posting to the forum you need to double the number of backslashes. See MUSHclient's Edit menu -> Convert clipboard forum codes. That will do it for you (to text you have on the clipboard).
#3
Nick Gammon said:

If you are posting to the forum you need to double the number of backslashes. See MUSHclient's Edit menu -> Convert clipboard forum codes. That will do it for you (to text you have on the clipboard).

Nice feature! I'll do that tonight, thank you.
Australia Forum Administrator #4
I'm assuming you didn't do that, based on the look of the rest of your raw post, so this is your trigger:


<triggers>
  <trigger
   enabled="y"
   match="\&lt;(\d+)\/(\d+)Hp (\d+)\/(\d+)mn (\d+)\/(\d+)mv\&gt; \[\d\|.+\] \((.*)\)"
   regexp="y"
   script="ProcessPrompt"
   sequence="100"
  >
  </trigger>
</triggers>


With the extra forum backslashes (almost as annoying as the password requirements, eh?) you can see that the square brackets are indeed escaped.

I changed your script to read:



function ProcessPrompt(name, text, wildcards)
    print("currentHealth", tonumber(wildcards[1]))
    print("maxHealth", tonumber(wildcards[2]))
    print("currentMana", tonumber(wildcards[3]))
    print("maxMana", tonumber(wildcards[4]))
    print("currentMovement", tonumber(wildcards[5]))
    print("maxMovement", tonumber(wildcards[6]))
    print("target", wildcards[7])
    print(wildcards[7])
    print("waitingForPrompt", "false")
end



On your test data I can't reproduce your problem:



<938/941Hp 251/251mn 583/586mv> [0|Witch Wood] (a goblin)

currentHealth 938
maxHealth 941
currentMana 251
maxMana 251
currentMovement 583
maxMovement 586
target a goblin
a goblin
waitingForPrompt false

<938/941Hp 251/251mn 583/586mv> [0|Witch Wood] ()

currentHealth 938
maxHealth 941
currentMana 251
maxMana 251
currentMovement 583
maxMovement 586
target 

waitingForPrompt false



Added blank lines for clarity. You can see that "a goblin" is recognized. Note that Tell does not show a newline, so with Tell there rather than print you won't see anything until you display something else with a newline in it.

Template:version
Please help us by advising the version of MUSHclient you are using. Use the Help menu -> About MUSHclient.