How do I ensure that a regex does not match an empty string?

Posted by Dancrumb on Stack Overflow See other posts from Stack Overflow or by Dancrumb
Published on 2010-03-12T04:04:44Z Indexed on 2010/03/12 4:07 UTC
Read the original article Hit count: 268

Filed under:
|
|

I'm using the Jison parser generator for Javascript and am having problems with my language specification.

The program I'm writing will be a calculator that can handle feet, inches and sixteenths. In order to do this, I have the following specification:

%%
([0-9]+\s*"'")?\s*([0-9]+\s*"\"")?\s*([0-9]+\s*"s")? {return 'FIS';}
[0-9]+("."[0-9]+)?\b  {return 'NUMBER';}
\s+                   {/* skip whitespace */}
"*"                   {return '*';}
"/"                   {return '/';}
"-"                   {return '-';}
"+"                   {return '+';}
"("                   {return '(';}
")"                   {return ')';}
<<EOF>>               {return 'EOF';}

Most of these lines come from a basic calculator specification. I simply added the first line.

The regex correctly matches feet, inch, sixteenths, such as 6'4" (six feet, 4 inches) or 4"5s (4 inches, 5 sixteenths) with any kind of whitespace between the numbers and indicators.

The problem is that the regex also matches a null string. As a result, the lexical analysis always records a FIS at the start of the line and then the parsing fails.

Here is my question: is there a way to modify this regex to guarantee that it will only match a non-zero length string?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex