Regex Questions

by Unknown

Back to Mechanic's Corner.

Unknown2010-01-08 14:16:21
I'm thinking that a thread for regex questions might be a good idea, since we now have three or more front end programs that use regex which is great for MUD related things smile.gif

And of course, this occurred to me because I have a regex question.

Here's the text I want to match:
Path: up, north, down, northeast, south

That's just an example, but I'm trying to capture the directions from the in-game path display. If I can capture all of the directions, I could build my own speedwalking script that is faster than the game's, for after I get a mount that can trot and gallop, while still using the in-game mapper for it's good maps, without having to go through the work of making an imap suspicious.gif

However, the regex to do this has eluded me. I've tried lots of variants of:
^Path: (?sad.gif\\w+), )+

but to no avail. Has anyone else done this or knows how to do it?
Zallafar2010-01-08 14:27:05
I use TinyFugue so I don't know if my regexp works the same as other people's. But if there were, say, a maximum of 50 directions, I would need to put in 50 sets of parenthesized expressions so that 50 variables could be assigned all the directions. You could match the whole thing with your approach but the directions wouldn't get assigned to variables that you could use.
Ilyarin2010-01-08 14:53:19
The easiest way is to use a function that splits the string into a table ", "

Trigger pattern:
CODE
^Path\\: (.+)$


In Lua, this function could be used (returns a table)
CODE
function string:split(delimiter)
  local result = { }
  local from  = 1
  local delim_from, delim_to = string.find( self, delimiter, from  )
  while delim_from do
    table.insert( result, string.sub( self, from , delim_from-1 ) )
    from  = delim_to + 1
    delim_from, delim_to = string.find( self, delimiter, from  )
  end
  table.insert( result, string.sub( self, from  ) )
  return result
end
Ssaliss2010-01-08 15:28:38
The only way to catch it into variables directly (i.e. not through a function like Ilyarins) would be to use "Path: (+)(, (+))?(, (+))?(, (+))?(, (+))?(, (+))?" etc. It's far better to use a way such as Ilyarins, but personally I'd alter that regex to "Path: ((west|east|north|...)(, west|, east|, north|...)*)" to make sure it doesn't catch anything weird. I'm probably overly cautious in that way though biggrin.gif

And of course, the "..." is just because I'm too lazy to write the directions.
Unknown2010-01-08 22:34:12
Yikes, I was expecting this to be simple. But my research kept turning up some kind of non-regex function called "split" and no other answers for something that seems like it would be easy, so I guess I will have to learn tables. Thanks guys content.gif
Vadi2010-01-08 23:44:28
Aw, oops. I thought it was exits, but a path is a path. Mudlet does have a split function, so capturing all the exits into a variable for example path and doing exits_table = path:split(",") should do it.
Unknown2010-01-09 04:23:33
QUOTE (Vadi @ Jan 9 2010, 12:44 AM) <{POST_SNAPBACK}>
Aw, oops. I thought it was exits, but a path is a path. Mudlet does have a split function, so capturing all the exits into a variable for example path and doing exits_table = path:split(",") should do it.

Good lord that was easy. I was once a skeptic but am rapidly becoming a Vadi fanboy. <3