How to write this regular expression in Lua?

前端 未结 2 1469
终归单人心
终归单人心 2021-01-05 03:26

I\'m new to the Lua regex equivalence features, I need to write the following regular expression, which should match numbers with decimals

\\b[0-9]*.\\b[0-9         


        
相关标签:
2条回答
  • 2021-01-05 03:43

    "%d*%.?%d+" will match all such numbers in decimal format (note that that's going to miss any signed numbers such as -1.1 or +3.14). You'll need to come up with another solution to avoid instances that end with ], such as removing them from the string before looking for the numbers:

    local pattern = "%d*%.?%d+"
    local clean = string.gsub(orig ,pattern .. "%]", "")
    return string.gmatch(clean, pattern)
    
    0 讨论(0)
  • 2021-01-05 03:52

    Lua does not have regular expressions, mainly because a full regular expression library would be bigger than Lua itself.

    What Lua has instead are matching patterns, which are way less powerful (but still sufficient for many use cases):

    • There is no "word boundary" matcher,
    • no alternatives,
    • and also no lookahead or similar.

    I think there is no Lua pattern which would match every possible occurrence of your string, and no other one, which means that you somehow must work around this.

    The pattern proposed by Stuart, %d*%.?%d*, matches all decimal numbers (with or without a dot), but it also matches the empty string, which is not quite useful. %d+%.?%d* matches all decimal numbers with at least one digit before the dot (or without a dot), %d*%d.?%d+ matches all decimal numbers with at least one digit after the dot (or without a dot). %.%d+ matches decimal numbers without a digit before the dot.

    A simple solution would be to search more than one of these patterns (for example, both %d+%.?%d* and %.%d+), and combine the results. Then look at the places where you found them and look if there is a ']' following them.


    I experimented a bit with the frontier pattern.

    The pattern %f[%.%d]%d*%.?%d*%f[^%.%d%]] matches all decimal numbers which are preceded by something that is neither digit nor dot (or by nothing), and followed by something that is neither ] nor digit nor dot (or by nothing). It also matches the single dot, though.

    0 讨论(0)
提交回复
热议问题