1

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?

hwnd
  • 69,796
  • 4
  • 95
  • 132
Enigma
  • 749
  • 1
  • 13
  • 35

2 Answers2

1

To include all special characters in regular expression in JFlex

i think its easier to exclude the numbers, letter, spaces and tabs instead of mentioning to all other possibilities . using this regular expression :

[^0-9a-zA-Z\n\t ]?
0

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

hwnd
  • 69,796
  • 4
  • 95
  • 132
  • Worked at me. Just \ missing for " , ' and ^. – Enigma Sep 09 '14 at 05:28
  • but inside a character class there are only 5 special chars; all chars lose their special meaning except `[`, `]`, `^` when first, `-` when not first or last and of course the backslash. – Bohemian Sep 09 '14 at 06:15
  • @Bohemian That is correct, with the exception of metacharacter sequences and ascii character classes. – Unihedron Sep 09 '14 at 06:34