[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Variable lines_to_match or reverse multiline regex (?)

Variable lines_to_match or reverse multiline regex (?)

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Woodspiral   (11 posts)  [Biography] bio
Date Thu 22 Jul 2021 09:40 PM (UTC)
Message
I'm trying to write a multiline regex to match our MUD's roomname, description and exits in one shot. I've tested the regex on regex101.com and you can see that in action here (https://regex101.com/r/o0AzVu/1/).

The regex works but the problem is that it matches too much. The essence of the issue is that I need the description to form a unique hash (I don't have the room's vnums, and room names and exits are not unique), and the room desc is not a fixed number of lines, it's like this (e.g.):

Room Name (inside)
desc 1
desc 2
...
desc n
[Exits: north south]

So the trigger I've written is like this:

<trigger
enabled="y"
lines_to_match="30"
keep_evaluating="y"
match="\A(.*) \((inside|city|field|forest|hills|mountain|swim|noswim|air|desert|underground|swamp|moor|space|underwater|small-fire|fire|big-fire|cold|acid|lightning|holy|evil|jungle|path|road|plain)\)([\s\S]+)\[Exits\: ([^\]]+)\]\Z"
multi_line="y"
regexp="y"
script="got_room_name"
sequence="100"
>
</trigger>

Whose regex works on regex101.com but NOT AT ALL on MUSH. If I replace \Z with say .* then it starts to work but it matches too many lines - it will match the previous 30 lines in the buffer which will include the previous room I was in before moving to the current one, so it all gets very confused very quickly.

How can I limit the regex to just new lines received since the last match or perhaps I should write the regex so it searches backwards? Perhaps there is a special character I can match which will anchor it to the end of the input from the MUD, like an EOF or something?

Perhaps I'm going about this the wrong way?

In mudlet I remember doing this by matching the room name - that would trigger a gate/filter thing which would suck up all the new lines received until the Exits line appeared whereby the gate/filter thing would close. Perhaps there is some similar mechanism in MUSH or maybe I could simulate that?

Thank you for your help,
Woody.
[Go to top] top

Posted by Fiendish   USA  (2,513 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Thu 22 Jul 2021 10:58 PM (UTC)

Amended on Fri 23 Jul 2021 02:45 AM (UTC) by Fiendish

Message
Quote:
In mudlet I remember doing this by matching the room name - that would trigger a gate/filter thing which would suck up all the new lines received until the Exits line appeared whereby the gate/filter thing would close.

If I understand your use of "gate", that is what I usually do for situations like this.

Quote:
Perhaps there is some similar mechanism in MUSH or maybe I could simulate that?

You can easily have three triggers in MUSHclient, one trigger for the room name, one trigger for _everything_ (that starts out disabled), and one trigger for the exits line (that also starts out disabled) that has an earlier sequence number than the everything trigger. Then you can have the triggers enable and disable each other and themselves at the right moments using the EnableTrigger function. Like you could have the room trigger enable the everything and exits triggers. And then you could have the exits trigger disable the everything trigger and itself.

Back to your regex though. I think you have a greediness problem with your pattern. As an example paste the room description several times into the test box and you'll see why it doesn't just match one room's description.

What if you do this pattern instead? (I added a couple question marks to make the growing pattern segments non-greedy.)

\A(.*?) \((inside|city|field|forest|hills|mountain|swim|noswim|air|desert|underground|swamp|moor|space|underwater|small-fire|fire|big-fire|cold|acid|lightning|holy|evil|jungle|path|road|plain)\)([\s\S]+?)\[Exits: ([^\]]+)\]

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,966 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Fri 23 Jul 2021 04:02 AM (UTC)
Message
Template:faq=37 Please read the MUSHclient FAQ - point 37.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Woodspiral   (11 posts)  [Biography] bio
Date Reply #3 on Fri 23 Jul 2021 06:53 AM (UTC)
Message
Thanks, Fiendish.

Fiendish said:

Back to your regex though. I think you have a greediness problem with your pattern. As an example paste the room description several times into the test box and you'll see why it doesn't just match one room's description.

What if you do this pattern instead? (I added a couple question marks to make the growing pattern segments non-greedy.)

\A(.*?) \((inside|city|field|forest|hills|mountain|swim|noswim|air|desert|underground|swamp|moor|space|underwater|small-fire|fire|big-fire|cold|acid|lightning|holy|evil|jungle|path|road|plain)\)([\s\S]+?)\[Exits: ([^\]]+)\]



Yes, that's the exactly the problem, but I need to anchor it at the end not the beginning of the text. The non-greedy markers you've suggested gobble up the penultimate room in the last 30 lines (configured in the trigger) but not the last room, which is what the mapper needs.

I read into it a little but didn't find anything that could easily fix this. Maybe I'm overlooking something (has been known!) What would fix it would be a multi-line equivalent of $ to signal EOF, which I could stick at the end of the regex. I tried \z and \Z at the end, which I said, has not worked (it doesn't match at all for some reason).
[Go to top] top

Posted by Woodspiral   (11 posts)  [Biography] bio
Date Reply #4 on Fri 23 Jul 2021 07:41 AM (UTC)
Message
Nick Gammon said:

(faq=37)


Oops yes, missed that sorry. It's looking like the three trigger approach is going to be the one to use here.

Thank you,
Woody.
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


9,722 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]