1

I can to use flex regular expression to find a match on "hello world" with double quote, the flex regular expression would be ["][^"\n]*["], as the result, I could get match case as hello world string without double quote. However, what happen if the string in "hello"""world", how can I use the flex regular expression to get the result as hello"""world? thanks.

dvdgsng
  • 1,691
  • 16
  • 27
rayling
  • 11
  • 3

1 Answers1

0

You can solve this by allowing double quotes as an alternative to any single character in the string. At the moment you have a character in the string as [^"\n]. To expand that to allow the double quotes as an alternative you write [^"\n]|\"\". A complete example flex program would look like this:

stringchar  ([^\"\r\n]|\"\")
ws           [ \t\r\n]+
%%
{ws}    printf("Whitespace: \"%s\"\n",yytext);
\"{stringchar}*\"   printf("String: %s\n",yytext);
[^ \t\r\n]+ printf("Not a string: \"%s\"\n",yytext);
%%

You should also note that flex has problems with matching adjoining tokens (see How to make lex/flex recognize tokens not separated by whitespace? ) for details. This means that "Hello"World" gets matched as "Not a String" and not a String followed by "Not a String".

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129