1

I am trying to differentiate if a user's input is a float or an integer. However, when a user enters a float, the whole input is considered a float but the decimal part of it is considered an integer, too. I don't want the decimal part to be considered an integer.

The user's input are added to a table.

We are making an interpreter for LOLCode. We are having a hard time on how to do the variable declaration and initialization.

if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.test(entry)){
 if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.test(entry)){
  var desc = "Variable Declaration";
  var lex = /I\sHAS\sA/i.exec(entry);
  addToLexer(lex, desc);
 }
 if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.test(entry)){
  var string = /(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.exec(entry);
  var desc = "Variable name";
  var lex = /[^(I\sHAS\sA)\s]+/.exec(string);
  addToLexer(lex, desc);
 }

 if(/.+ITZ\s.*/i.test(entry)){
  var string = /.+ITZ\s.*/i.exec(entry);
  var desc = "Initialization";
  var lex = /ITZ/i.exec(string);
  addToLexer(lex, desc);
 }
}

if(/^[-+]?\d+$/i.test(entry)){
 var desc = "Integer Literal";
 var lex = /[-+]?\d+$/i.exec(entry);
 addToLexer(lex, desc);
}

if(/^[-+]?\d*\.\d*$/i.test(entry)){
 var desc = "Float Literal";
 var lex = /[-+]?[0-9]*\.?[0-9]+$/i.exec(entry);
 addToLexer(lex, desc);
}

Our LOLCode Interpreter

[EDITED] After doing one of the suggestions, the whole number part of the float is the one considered as another integer now.

Interpreter after the edit

Xeonel
  • 21
  • 2
  • `^` (the beginning of the string anchor)? – zerkms Nov 28 '16 at 08:35
  • Aren't you looking for this ? http://stackoverflow.com/questions/3885817/how-do-i-check-that-a-number-is-float-or-integer – Juan Picado Nov 28 '16 at 08:38
  • We're making an interpreter for LOLCode. When we add a `^` , the literals in `I HAS A [variable_name] ITZ [value]` lines are not caught at all – Xeonel Nov 28 '16 at 08:40
  • @JuanPicado We need to use regex...? – Xeonel Nov 28 '16 at 08:42
  • Use `^` too: `/^[-+]?\d+$/i`, and `/^[-+]?\d*\.\d+$/i` (note I'd suggest to adjust the float regex so that the last `\d` could match at leat 1 digit), and `/^[-+]?[0-9]*\.?[0-9]+$/i` – Wiktor Stribiżew Nov 28 '16 at 08:48
  • @WiktorStribiżew Adding `^` does work, but what if the literals are not the only ones in a line of code? They are not caught anymore – Xeonel Nov 28 '16 at 08:52
  • Then replace `^` and `$` with `\b`. Use `/[-+]?\b\d+\b/i`, `/[-+]?\d*\.\d+\b/i` and `/[-+]?\b[0-9]*\.?[0-9]+\b/i`. Well, the float regex is a bit trickier, especially if you need to match `.33` like floats. Instead of a trailing `\b` a `(?!\d)` might prove better. Please provide some texts to test against. – Wiktor Stribiżew Nov 28 '16 at 08:56
  • @WiktorStribiżew It didn't work ;-; Now, even the _integers_ are considered _float_ and the whole number part of the _float_ is an integer too. – Xeonel Nov 28 '16 at 09:07
  • @Xeonel you said "However, when a user enters a float, the whole input is considered a float but the decimal part of it is considered an integer, too" you mean it is considered to be an integer as part of the requirement, or considered to be an integer by your code? What exactly is the requirement? – nonopolarity Nov 28 '16 at 09:08
  • @太極者無極而生 Considered integer by my code. It shouldn't be that way though. After doing WiktorStribiżew's suggestion, the whole number part of the is the one considered as another integer now. – Xeonel Nov 28 '16 at 09:12
  • Ok, INT regex - [`(^|[^\d.])\b\d+\b(?!\.\d)`](https://regex101.com/r/ZPmKCS/1) and a FLOAT regex - [`(^|[^\d.])\d*\.\d+\b(?!\.\d)`](https://regex101.com/r/ZPmKCS/2). – Wiktor Stribiżew Nov 28 '16 at 09:14
  • @WiktorStribiżew Oh my God! It works! Thank you so much! TTwTT – Xeonel Nov 28 '16 at 09:19
  • I just joined Stack Overflow today, how do I make my question as answered? – Xeonel Nov 28 '16 at 09:21

1 Answers1

0

Here are the patterns you need to use to differentiate between FLOAT/DECIMAL and INTEGER values:

/(^|[^\d.])\b\d+\b(?!\.\d)/      

to find integer values in a long string, see demo.

To extract floats, use

/(^|[^\d.])\d*\.\d+\b(?!\.\d)/

See this demo.

If the input is multiline, do not forget to add /m modifier.

Details:

  • (^|[^\d.]) - a start of string anchor (^) or (|) any char but a digit and . (see the negated character class [^\d.])
  • \b - leading word boundary
  • \d+ - 1+ digits
  • \b - trailing word boundary
  • (?!\.\d) - a negative lookahead failing the match if there is a dot followed with a digit

The float regex is almost the same but it requires a . before a digit and it does not need a leading word boundary.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563