3

So I am trying to get string.match to not only get '%d+'(any string of strait digits) but I want it to include decimal points IF they are present. I can't make the pattern '.%d+' or '%d+.%d+' because then those would detect any punctuation and wouldn't get a plain non decimal number

I'm so lost here

  • You have to try regular expressions from answer here http://stackoverflow.com/questions/2811031/decimal-or-numeric-values-in-regular-expression-validation and remove this question - it's effectively a duplicate, but for particular technology (lua) – Petro Korienev Aug 11 '14 at 16:52
  • 1
    @PetroKorienev Lua does not have regex in traditional sense. Lua patterns will not recognise `(%.%d+)?` (where `%` acts the same as `\ ` does in normal patterns) – hjpotter92 Aug 11 '14 at 18:36
  • Oh, sorry then :( Then, maybe it has sense to try something like http://rrthomas.github.io/lrexlib/ to get power of POSIX regexps? – Petro Korienev Aug 12 '14 at 06:02

2 Answers2

5

Try the pattern "(%d*%.?%d+)"

It matches:

  • 12345
  • 12.345
  • .12345

But not:

  • 12345.
daurnimator
  • 4,091
  • 18
  • 34
0

You could also use [%d%.]+ if you are sure that decimal points only appear with numbers. This pattern matches

  • 12
  • 12.
  • 12.3
  • .3

but also

  • . without any nearby digits
cad
  • 347
  • 5
  • 20