忽略tmLanguage定义中的换行符

时间:2013-12-06 17:01:13

标签: regex textmate sublimetext

我有一个tmLanguage定义(对于一个笨重的8088模拟器)并且一些正则表达式变得非常大。

<string>\s*(?i)(%define|%ifndef|%xdefine|%idefine|%undef|%assign|%defstr|%strcat|%strlen|%substr|%00|%0|%rotate|%rep|%endrep|%include|\$\$|\$|%unmacro|%if|%elif|%else|%endif|%(el)?ifdef|%(el)?ifmacro|%(el)?ifctx|%(el)?ifidn|%(el)?ifidni|%(el)?ifid|%(el)?ifnum|%(el)?ifstr|%(el)?iftoken|%(el)?ifempty|%(el)?ifenv|%pathsearch|%depend|%use|%push|%pop|%repl|%arg|%stacksize|%local|%error|%warning|%fatal|%line|%!|%comment|%endcomment|__NASM_VERSION_ID__|__NASM_VER__|__FILE__|__LINE__|__BITS__|__OUTPUT_FORMAT__|__DATE__|__TIME__|__DATE_NUM__|_TIME__NUM__|__UTC_DATE__|__UTC_TIME__|__UTC_DATE_NUM__|__UTC_TIME_NUM__|__POSIX_TIME__|__PASS__|ISTRUC|AT|IEND|BITS16|BITS32|BITS64|USE16|USE32|__SECT__|ABSOLUTE|EXTERN|GLOBAL|COMMON|CPU|FLOAT|_STDOUT|_GETCHAR|_WRITE|_STDIN|_EXIT|\.SECT\.....?)\b?([_a-zA-Z][_a-zA-Z0-9]*)?</string>

我想,好吧,我会把它分成多行。

<string>\s*(?i)(
    %define|%ifndef|%xdefine|%idefine|%undef|%assign|%defstr|%strcat
    |%strlen|%substr|%00|%0|%rotate|%rep|%endrep|%include|\$\$|\$
    |%unmacro|%if|%elif|%else|%endif|%(el)?ifdef|%(el)?ifmacro
    |%(el)?ifctx|%(el)?ifidn|%(el)?ifidni|%(el)?ifid|%(el)?ifnum
    |%(el)?ifstr|%(el)?iftoken|%(el)?ifempty|%(el)?ifenv|%pathsearch
    |%depend|%use|%push|%pop|%repl|%arg|%stacksize|%local|%error
    |%warning|%fatal|%line|%!|%comment|%endcomment
    |__NASM_VERSION_ID__|__NASM_VER__|__FILE__|__LINE__|__BITS__
    |__OUTPUT_FORMAT__|__DATE__|__TIME__|__DATE_NUM__|_TIME__NUM__
    |__UTC_DATE__|__UTC_TIME__|__UTC_DATE_NUM__|__UTC_TIME_NUM__
    |__POSIX_TIME__|__PASS__|ISTRUC|AT|IEND|BITS 16|BITS 32|BITS 64
    |USE16|USE32|__SECT__|ABSOLUTE|EXTERN|GLOBAL|COMMON|CPU|FLOAT
    |_STDOUT|_GETCHAR|_WRITE|_STDIN|_EXIT|\.SECT \.....?)\b ?([_a-zA-Z][_a-zA-Z0-9]*)?
</string>

除此之外,当我将其分解时,正则表达式会尝试匹配,例如%strcat\n(在行的末尾添加换行符。)

我不想那样。

有没有办法忽略此保存的正则表达式中的换行符

1 个答案:

答案 0 :(得分:0)

您可以使用正则表达式格式应用http://www.regexformat.com来自压缩和格式化的

修改并进行一些重构,可能就是这个

 # <string>\s*(?i)(%define|%ifndef|%xdefine|%idefine|%undef|%assign|%defstr|%strcat|%strlen|%substr|%00|%0|%rotate|%rep|%endrep|%include|\$\$?|%unmacro|%if|%elif|%else|%endif|(?:%(?:el)?(?:ifdef|ifmacro|ifctx|ifidn|ifidni|ifid|ifnum|ifstr|iftoken|ifempty|ifenv))|%pathsearch|%depend|%use|%push|%pop|%repl|%arg|%stacksize|%local|%error|%warning|%fatal|%line|%!|%comment|%endcomment|__NASM_VERSION_ID__|__NASM_VER__|__FILE__|__LINE__|__BITS__|__OUTPUT_FORMAT__|__DATE__|__TIME__|__DATE_NUM__|_TIME__NUM__|__UTC_DATE__|__UTC_TIME__|__UTC_DATE_NUM__|__UTC_TIME_NUM__|__POSIX_TIME__|__PASS__|ISTRUC|AT|IEND|BITS16|BITS32|BITS64|USE16|USE32|__SECT__|ABSOLUTE|EXTERN|GLOBAL|COMMON|CPU|FLOAT|_STDOUT|_GETCHAR|_WRITE|_STDIN|_EXIT|\.SECT\.....?)(?-i)\b?([_a-zA-Z][_a-zA-Z0-9]*)?</string>

 <string>(?x)              # Expanded mode (ignore whitespace) - first chars of the regex string 
 \s* 
 (?i)
 (                                  # (1 start)
      %define
   |  %ifndef
   |  %xdefine
   |  %idefine
   |  %undef
   |  %assign
   |  %defstr
   |  %strcat
   |  %strlen
   |  %substr
   |  %0 0?
   |  %rotate
   |  %rep
   |  %endrep
   |  %include
   |  \$ \$?
   |  %unmacro
   |  %if
   |  %elif
   |  %else
   |  %endif
   |  %
      (?: el )?
      (?:
           ifdef
        |  ifmacro
        |  ifctx
        |  ifidn
        |  ifidni
        |  ifid
        |  ifnum
        |  ifstr 
        |  iftoken
        |  ifempty
        |  ifenv 
      )
   |  %pathsearch
   |  %depend
   |  %use
   |  %push
   |  %pop
   |  %repl
   |  %arg
   |  %stacksize
   |  %local
   |  %error
   |  %warning
   |  %fatal
   |  %line
   |  %!
   |  %comment
   |  %endcomment
   |  __NASM_VERSION_ID__
   |  __NASM_VER__
   |  __FILE__
   |  __LINE__
   |  __BITS__
   |  __OUTPUT_FORMAT__
   |  __DATE__
   |  __TIME__
   |  __DATE_NUM__
   |  _TIME__NUM__
   |  __UTC_DATE__
   |  __UTC_TIME__
   |  __UTC_DATE_NUM__
   |  __UTC_TIME_NUM__
   |  __POSIX_TIME__
   |  __PASS__
   |  ISTRUC
   |  AT
   |  IEND
   |  BITS16
   |  BITS32
   |  BITS64
   |  USE16
   |  USE32
   |  __SECT__
   |  ABSOLUTE
   |  EXTERN
   |  GLOBAL
   |  COMMON
   |  CPU
   |  FLOAT
   |  _STDOUT
   |  _GETCHAR
   |  _WRITE
   |  _STDIN
   |  _EXIT
   |  \.SECT\. . . . .? 
 )                                  # (1 end)
 (?-i)
 \b? 
 ( [_a-zA-Z] [_a-zA-Z0-9]* )?       # (2)
 </string>