Jump to content




Matching a conventional Lua number

lua

5 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 14 January 2017 - 10:34 PM

Hey guys,
as the title says, i wanna match a conventional Lua number, like 10 or 10.0 or 10.6e12.
I thought out such a pattern:
'[+-]?%d-[%.]?%d-?[eE]?',
but it doesn't seem perfect.
Do you guys know a common pattern?
Thanks in advance

Edited by Sewbacca, 19 January 2017 - 12:59 PM.


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 15 January 2017 - 12:36 AM

Things like + and - have meaning in patterns and must be escaped. I'd try something like:

"[%+%-]?%d+%.?%d*%-?[eE]?%d*"


#3 Exerro

  • Members
  • 801 posts

Posted 16 January 2017 - 01:32 PM

You can't actually match it with one pattern, sadly. Lyqyd's suggestion ("[%+%-]?%d+%.?%d*%-?[eE]?%d*") is close, but it won't match '.1' and it will match '1.3e'.

You should should use the following instead:
str:match "[%+%-]?%d*%.?%d+[eE][%+%-]%d+" or str:match "[%+%-]?%d*%.?%d+"

Edited by Exerro, 16 January 2017 - 01:32 PM.


#4 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 16 January 2017 - 06:25 PM

I tried your suggestions and got the following result:
"[%+%-]?%d+%.?%d*%-?[eE]?%d*" (Lyquid's) accepted -1-11 (could he prevent that) and 1e
"[%+%-]?%d*%.?%d+[eE][%+%-]%d+" (Exerro 1th one) doesn't accepted something like 1e[+-]?1.1 so
"[%+%-]?%d*%.?%d+[eE][%+%-]%d*%.?%d+" should do it.
"[%+%-]?%d*%.?%d+" seemed to work fine.

#5 Exerro

  • Members
  • 801 posts

Posted 17 January 2017 - 07:44 PM

It was intentional to not allow decimals after the 'e':
Posted Image
I doubt LuaJ/CC's Lua would be any different.

'5e1' means '5 * 10 ^ 1', so '5e1.1' would mean '5 * 10 ^ 1.1', which is just a bit pointless. I never use it myself, but if you're doing anything measurementy it'll come in handy, e.g. the speed of light '3e8'.

Edited by Exerro, 17 January 2017 - 07:47 PM.


#6 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 17 January 2017 - 09:25 PM

View PostExerro, on 17 January 2017 - 07:44 PM, said:

It was intentional to not allow decimals after the 'e':
Posted Image
I doubt LuaJ/CC's Lua would be any different.

'5e1' means '5 * 10 ^ 1', so '5e1.1' would mean '5 * 10 ^ 1.1', which is just a bit pointless. I never use it myself, but if you're doing anything measurementy it'll come in handy, e.g. the speed of light '3e8'.

Oh thanks ^^.

Edited by Sewbacca, 17 January 2017 - 09:30 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users