2

I'm trying to create a Lua pattern that will help me retrieve a version number inside a docblock or similar multiline string/comment.

Right now this is how it looks:

s = [[/** 
      * Let's pretend this is a random docblock...
      * 
      * Very special line, super cool. Does many things.
      *
      * @version: 1.2.3
      * @author: Unknown
      */]]

local match = string.match(s, "@version%p%s%d%p%d")
print(match)

Running this code yields:

@version: 1.2

What I really want is a pattern that will match any common version numbering and this is where I hit a brick wall since Regex and Lua patterns is something I simply never seem to learn. Is this possible with Lua patterns and the string.match function?

(Will be forever in your debt if you have a "dummies guide to patterns/regex")

4
  • Tutorial at lua-users. You can try to match @version%p%s+[^\n]+. Commented Feb 28, 2014 at 21:28
  • Or you can simply look into LuaDoc Commented Feb 28, 2014 at 21:29
  • @hjpotter92 Thank you! I will look up LuaDoc, but that is just for Lua-specific comments right? My main project with this is to read large php-files and get their version number from a docblock, can LuaDoc help with that? Commented Feb 28, 2014 at 21:34
  • There should be similar documentation generator for php code as well. Commented Feb 28, 2014 at 21:51

1 Answer 1

1

Note that "will match any common" is very broad! But if you don't need do process the version number, for instance you just want to print the version line for each file, then all you need is this:

local match = string.match(s, "@version:.-\n")
print(match) -- @version: 1.2.3rc1\n

You have to use .- instead of .* because your string has newlines, if you don't the match will extend to end of string.

If all you really want is the version string itself, and "@version:" never changes, then all you need is this:

local match = string.match(s, "@version:%s+(.-)")
print(match) -- prints 1.2.3rc1

If you want major and minor version numbers, then you have to specify "any common": major.minor.patch with multiple digits for each:

local match = string.match(s, "@version:%s+(%d+).(%d+).(%d+)(%w+)")
print(match) -- prints      1       2       3       rc1
Sign up to request clarification or add additional context in comments.

1 Comment

IMHO, "@version:%s+([^\r\n]*)" would be better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.