Skip to content

Issue with string.find in Mudlet

I'm writing a simple script to track changes in my experience using gmcp.Char.Status.level, but running into an issue with the string.find() function.
The output for gmcp.Char.Status.level for me at the moment is '90  (0.31%)', and since I'm only interested in the part between the '(' and '%' characters, I thought to use string.sub() to isolate it. The problematic bit of code in question is:

  string.sub(gmcp.Char.Status.level, string.find(gmcp.Char.Status.level, "(") + 1, string.find(gmcp.Char.Status.level, "%"))

This fails, with the error 'unfinished capture'. If I run the code:

  string.sub(gmcp.Char.Status.level, 6, 9)

It returns '0.31', as intended.

Running:

  string.find(gmcp.Char.Status.level, "(")

by itself results in the same 'unfinished capture' error, while running:

  string.find(gmcp.Char.Status.level, "%")

results in a new error that says 'malformed pattern (ends with '%'). I am unsure why this is happening, as running the exact same string.find() function for any other character in the string returns its position, including running it with ")".

If anyone could explain this to me, I would be most grateful.

Comments

  • edited January 2013
    String.find allows for pattern matching. ( and % are reserved characters in pattern matches (start of capture group and start of pattern symbol, respectively)).

    Escaping those characters should work.

    EDIT: For more on patterns and captures, take a look at the patterns tutorial on the lua users wiki.
    I am the righteous one... 
    the claims are stated - it's the world I've created 
  • Thanks a lot, it works perfectly. I had tried this earlier, but I made this mistake of thinking the escape character was '\' instead of '%'.
Sign In or Register to comment.