所有文本的正则表达式以“#”开头但不包含#define

时间:2011-09-11 18:45:16

标签: regex lex

我正在使用的正则表达式是在flex中实现预处理器。 这个预处理器很简单。 它遵守以下规则:

  1. 预处理程序指令以#define开头,后跟大写字母标识符
  2. 预处理程序标识符的用例以#符号开头。
  3. 例如:

    #define CONSTANT 100
    //...
    int x = #CONSTANT;
    

    所以我做的第一件事就是

    #define {
               //store the identifier following #define in a lookup table 
               //do the relevant error checking 
            }
    NO_POUND_DEFINE {
                      //The string should begin with a '#' sign but not with `#define`
                      //check if the string following '#' is upper case or not
                      //if in upper case do the lookup otherwise throw an error
                    }
    

2 个答案:

答案 0 :(得分:2)

var regexp = /^((?!#define).)*$/;

也许你想看看这个:regular-expression-to-match-string-not-containing-a-word

答案 1 :(得分:1)

^#([^d]|d[^e]|de[^f]|def[^i]|defi[^n]|defin[^e]).*字符串以“#”开头,​​后跟“d”或后跟“d”,但后面没有“e”等。