当前位置:编程学习 > 网站相关 >>

Tex中的正则表达式替换

本人在用Tex写论文时,碰到要将\textbf{NumEQ},\textbf{NumBC},\textbf{Err[k]}这样的字符串全部相应地转换成\verb|NumEQ|, \verb|NumBC|, \verb|Err[k]|。因为文章中有大量地方需要修改,手动修改机耗时又可能漏掉,最终采用正则表达式替换修改。
 
对被替换的字符换描述为:
[plain] 
\\textbf\{\(*\)\}  
 
转化后的字符串描述为:
[plain] 
\\verb\|\0\|  
 
说明:
1.  \是转义字符,将下一个字符标记为一个特殊字符、或一个原义字符、或一个 向后引用、或一个八进制转义符。例如,'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\\' 匹配 "\" 而 "\(" 则匹配 "("。
2. *匹配任意字符串(包括空串)。www.zzzyk.com
3.  \(*\)\ 将这些匹配成功的字符串标记为\0,这样可以在之后的替换模式表达式中引用它。
4. \0 表示对前面匹配的引用。
在WinEdt中的样图如下:
 
 
注意:WinEdt中的正则表达式和GUN上的有区别,用时需要注意。
 
GNU vs. WinEdt
 
WinEdt regular expressions for GNU regular expressions users
 
by Denis Stancer
 
Philosophy
GNU regular expressions have mainly the following form:
  <qualifier><quantifier>
where qualifier means WHAT and quantifier means HOW MANY. GNU has only one quantifier:
  {m,n} =  minimally m times and maximally n times
There are several quantifier synonyms:
  * = {0,}
  + = {1,}
  ? = {0,1}
If a quantifier is left out it means {1,1}, i.e {1}.
 
WinEdt has the reverse philosophy. Its regular expressions are mainly:
  <quantifier><qualifier>
WinEdt has two quantifiers:
  + = 1 or more times
  @ = 0 or more times
 
Comparison
  NSM - No Special Meaning - any non-special expression
 
  Exp     GNU meaning                             WinEdt meaning
 
  .       Matches any single character            Matches any single character (possibly none)
  *       Prev.exp. must occur 0 or more times    Matches any string (including empty)
  +       Prev.exp. must occur 1 or more times    Matches any (non-zero) number of next exp.
  ?       Prev.exp. must occur 0 or 1 time        Matches any (non-zero) single character
  $       Matches end of a line                   (In pair) Alternative Set Definition
  ^       Matches the start of a line             not + Skip Next Pattern
  >       NSM                                     Matches the end of a line
  <       NSM                                     Matches the start of a line
  [ ]     Set                                     Set
  [-]     Range                                   Range
  \(\)    NSM - Matches ()                        Tagged Expression
                                                  (x in the range 0..9; 0 is a default)
  ()      Tagged Expression and group             NSM - Matches )
  \x      Tag backreference (0..9)                Tag backreference (0..9)
  $x      Tag (x in the range 0..9)               NSM - In fact error
  {n,m}   universal quantifier: {n,m} prev. exp   Group
          must occur min. n and max. m times
  \y      Matches character y itself (exl. 1-9)   Matches character y itself (exl. 0-9)
  |       Alternation (or)                        Alternation (or)
  @       NSM (matches @)                         Matches any number of occurrences of
                                                  the next expression
  \I      NSM (matches I)                         initial case in backreference \I1
  \T      NSM (matches T)                         toggle case in backreference \T1
  \t      tab                                     NSM (matches t)
  \n      newline                                 NSM (matches n)
  \r      return                                  NSM (matches r)
  \f      form feed                               NSM (matches a)
  \a      alarm (bell)                            NSM (matches e)
  \e      escape                                  NSM (matches r)
  \       octal char                              NSM (interpreted as \0})
  \       hex char                                NSM (matches )
  \       control char                            NSM (matches )
  \l      lowercase next char                     NSM (matches l)
  \u      uppercase next char                     NSM (matches u)
  \L      lowercase till \E                 &n
补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,