regexjflex

Special Characters in JFlex Regular Expression


I want to include all special characters in regular expression in JFlex. So I prepared one as below.

P = ("_"|"-"|"/"|"."|","|"~"|"!"|"@"|"#"|"$"|"%"|"^"|"&"|"*"|"|"|"("|")"|"="|"+"|"|"|"\"|":"|";"|"""|"<"|">"|"?"|"`"|"{"|"}"|"["|"]"|"'")
  1. Could somebody tell me is there any other way to cover all special characters in more optimized way?

  2. Also could you please point out what's wrong in above regex as it is giving me "Unterminated string at end of line." error on compilation?


Solution

  • To fix your problem, you need to escape the backslash \ with a backslash \\

    An easier way to define these characters would be a character class.

    [-/_.,~!@#$%^&*|(){}\[\]<>?=+\\:;"'`]
    

    You can keep adding characters you want to include to the class.

    Note: You can reference the special characters at http://www.regular-expressions.info/characters.html