本人在用Tex写论文时,碰到要将\textbf{NumEQ},\textbf{NumBC},\textbf{Err[k]}这样的字符串全部相应地转换成\verb|NumEQ|,\verb|NumBC|,\verb|Err[k]|。因为文章中有大量地方需要修改,手动修改机耗时又可能漏掉,最终采用正则表达式替换修改。


对被替换的字符换描述为:

  1. \\textbf\{\(*\)\}

转化后的字符串描述为:

  1. \\verb\|\0\|

说明:

1. \是转义字符,将下一个字符标记为一个特殊字符、或一个原义字符、或一个 向后引用、或一个八进制转义符。例如,'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\\' 匹配 "\" 而 "\(" 则匹配 "("。

2. *匹配任意字符串(包括空串)。

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}  ? 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                       lowercase backreference
  \L1
  \U      uppercase till \E                       uppercase backreference
  \U1
  \E      end case modification                   NSM (matches E)
  \Q      disable pattern Metacharacters          NSM (matches Q)
          till \E
  \w      Match a word (alphanumeric plus "_")    NSM (matches w)
  \W      Match a non-word character              NSM (matches W)
  \s      Match a whitespace character            NSM (matches s)
  \S      Match a non-whitespace character        NSM (matches S)
  \d      Match a digit character                 NSM (matches d)
  \D      Match a non-digit character             NSM (matches D)

Translation

  GNU quantifier      WinEdt quantifier

  {m,n}                   N/A
  *                       @
  +                       +
  ?                       N/A (use @)

  GNU qualifier       WinEdt qualifier

  |                       |
  .                       . or ?
  $                       >
  ^                       <
  ]                       ]
  ]                       ]
  []                      ~]
  )                       \(\)        (x in range 0..9)
  \x or $x                \x          (x in range 1..9 for GNU and 0..9 for WinEdt)
                                      (excluding 1..9 for GNU and 0..9 for WinEdt)
  \w                      $Alpha+Numeric+["_"]$
  \W                      $~(Alpha+Numeric+["_"])$
  \s                      $[" "]+[#9]+[#13]+[#10]$
  \S                      $~([" "]+[#9]+[#13]+[#10])$
  \d                      $Numeric$ or [0-9]
  \D                      $~Numeric$ or ~[0-9]
  \t                      $[#89]$
  \n                      $[#13]$ or >
  \r                      $[#13]$
  \f                      $[#10]$
  \a                      $[#]$
  \e                      $[#]$

  GNU modifier        WinEdt modifier

  \                       ???
  \                       ???
  \cX                     $[#x]$  (X is a letter and x is a number)
  \l                      N/A (use \Lx x in range 0..9)
  \u                      N/A (use \Ux x in range 0..9)
  \L                      N/A (use \Lx x in range 0..9)
  \U                      N/A (use \Ux x in range 0..9)
  \E                      N/A
  \Q                      N/A

Examples:

1.Replace any 3 letter word that has "at" at the end with fat:

  GNU                                         WinEdt
  Search for:   ".at"                         Search for:    ".at"
  Replace with: "fat"                         Replace with:  "fat"

2.Replace all 3.14 with Pi:

  GNU                                         WinEdt
  Search for:   "3\.14"                       Search for:    "3\.14"
  Replace with: "Pi"                          Replace with:  "Pi"

Note:"3.14" would do the same thing but it would also replace numbers like 3214 or 351432.

3.Replace all numeric Pi regardless of how many decimals it has:

  GNU                                         WinEdt
  Search for:   "3\.[0-9]+" or "3\.\d+"       Search for:    "3\.+[0-9]"
  Replace with: "Pi"                          Replace with:  "Pi"

4.Replace all methane and ethane with alcane:

  GNU                                         WinEdt
  Search for:   "m?etahne"                    Search for:    "@{m}ethane"
  Replace with: "alcane"                      Replace with:  "alcane"

5.Replace cyclopentane,isopentane,hexapentane with pentane but not n-pentane or dodecapentane:

  GNU                                         WinEdt
  Search for:   ".{3,5}pentane"               Search for:    "[cih]??{{..pentane}|{.pentane}|{pentane}}"
  Replace with: "pentane"                     Replace with:  "pentane"

6.Replace tan,man,can with try but not ban,fan or pan:

  GNU                                         WinEdt
  Search for:   "\b[cmt]an\b"                 Search for:    "{<|}[cmt]an{>| }"
  Replace with: "try"                         Replace with:  "try"

7.Replace any of possible variations of Christianssen surname (Kristianssen,Kristiansson,Christiansen,Kristenssen,...) with Xson:

  GNU                                            WinEdt
  Search for:   "(Ch|K)rist(ia|e)ns{1,2}[eo]n"   Search for:     "{Ch|K}rist{ia|e}ns@{s}[eo]n"
  Replace with: "Xson"                           Replace with:   "Xson"

8.Replace 2 or more empty lines with one:

  GNU                                            WinEdt
  Search for:   "\n\n+"                          Search for:     "<>+>"
  Replace with: "\n\n"                           Replace with:   ">"

9.Insert ; at the end of a non-empty line:

  GNU                                            WinEdt
  Search for:   "(\w)$"                          Search for:     "\(^<\)>"
  Replace with: "\1;"                            Replace with:   "\0;>"

10.Quote every non-empty line:

  GNU                                            WinEdt
  Search for:   "^([^\n]+)$"                     Search for:     "<\(+^>\)>"
  Replace with: ""\1""                           Replace with:   ""\0">"

11.Switch every two words:

  GNU                                            WinEdt
  Search for:   "(\w+) (\w+)"                    Search for:     "\(1+$Alpha$\) \(2+$Alpha$\)"
  Replace with: "$2 $1"                          Replace with:   "\2 \1"

12.Replaces all twice repeated instances of words (a-z) separated by an arbitrary number of spaces (even across line boundaries) and replaces them with a single word.

  GNU                                            WinEdt
  Search for:   "\b(\w+)\s+\1\b"                 Search for:     "\(+[a-z]\)@ @>@ \0"
  Replace with: "\1"                             Replace with:   "\0"

13.Replaces all repeated instances of words (a-z) separated by an arbitrary number of spaces (even across line boundaries) and replaces them with a single word.

  GNU                                            WinEdt
  Search for:   "\b(\w+)(\s+\1)+\b"              Search for:     "\(+[a-z]\)@ @>@ \0@{@ @>@ \0}"
  Replace with: "\1"                             Replace with:   "\0"

14.Escape all (La)TeX special characters:

  GNU                                            WinEdt
  Search for:   "(\\|\{|\}|\$|\&|\_|\^)"         Search for:     "\(\\|\{|\}|\$|\&|\_|\^\)"
  Replace with: "\\\1"                           Replace with:   "\\\0"

15.Capitalize all the words:

  GNU                                            WinEdt
  Search for:   "(\w+)"                          Search for:     "\(+$Alpha$\)"
  Replace with: "\u\L\1"                         Replace with:   "\I0"

16.Replace {x \over y} with \frac{x}{y}:

  GNU                                            WinEdt
  Search for:   "\{(.*) \\over (.*)\}"           Search for:     "\{\(0*\)\\over \(1*\)\}"
  Replace with: "\\frac\{\1\}\{\2\}"             Replace with:   "\\frac\{\0\}\{\1\}"

17.Find instances of word "label" without control prefix "\" and replace them with lbl:

  GNU                                            WinEdt
  Search for:   "(^|[^\\])label\b"               Search for:     "\(0{<|^[\\]}\)label~[a-z]"
  Replace with: "\1lbl"                          Replace with:   "\0lbl"

RegEx Examples

This section sets out some examples and includes a couple of answers to questions relating to regular expressions.

In the examples below,double quotes around the search or replacement string are not a part of the actual RegEx.

1.Find control characters in a document:

  Search for:   "$[#0..#31]$"

2.Find instances of the word "label" without the control prefix "\":

  Search for:   "#~{\\}label"

3.Find instances of "\begin",butnot"\begin{figure}":

  Search for:   "\\begin{~\{figure\}}"

4.Match nested brackets "(...(...)...)" (up to level 6):

  Search for:
    "(@{>|{(@{>|{(@{>|{(@{>|{(@{>|{(*)}|^{)}}*)}|^{)}}*)}|^{)}}*)}|^{)}}*)}|^{)}}*)"

Hint:^{expression} matcheseverything except"expression".This one is rather tricky...

5.RegEx Replace:

  "<>>" --> ">"

replaces all double empty lines with single ones. Note,however,that such an algorithm is quadratic in nature and will not perform very well on large files. Splitting the file into smaller segments might improve it. In order to avoid the quadratic factor,the proper solution would be to search for "<>>" and rebuild the single spaced output from scratch... However,the example will work fine on reasonably small files.

6.A RegEx Replace:

  "<??\>" --> "0??\>"

replaces a string "13>" positioned at the beginning of line with "013>",while the string "99>"; is replaced by "099>";. Tags should be used to perform more sophisticated replacements of this nature...

When replacing text using regular expressions,it is very useful to have a so-called "tagged expression"; i.e.,that part of the "find" RegEx that can be referred to in the "replace" RegEx.

"\(x<Expression>\)"brackets a tagged expression to use in replacement expressions. One RegEx can have up to ten tagged expressions. The corresponding replacement expression is\x,for x in the range 0-9. Tagged expressions cannot be nested. If you skip a tag index after"\("0 is assumed.

7.A RegEx Replace:

  "\(+[a-z]\) \(1+[a-z]\)" -> "\1 \0"

matches "way wrong",and replaces it with "wrong way".

8.A RegEx replace:

  "\(+[a-z]\)+ \0" -> "\0"

seeks all repeated instances of words (a-z) separated by an arbitrary number of spaces,and replaces them with a single word. If you want to detect words even if they are located in two lines,you can change the search string to:

  "\(+[a-z]\){{> }|{+ }|{+ >}|{>+ }|{+ >+ }}\0"
    or
  "\(+[a-z]\)@ @> @ \0"

Note the use of"+"(repeat) and"|"(or).

9.Replace "{x \over y}" with "\frac{x}{y}":

  Search for:   "\{\(0*\) \\over \(1*\)\}"
  Replace with: "\\frac\{\0\}\{\1\}"

10.Suppose you want to put the escape character "\" in front "\{}" inside \begin..\end environments. This RegEx replace:

  Search for:   "{\(0\\begin\)\(1\{*\)\}}|{\(0\\end\)\(1\{*\)\}}"
  Replace with: "\\\0\\\1\\\}"

will do this:

  \begin{equation}                   \\begin\{equation\}
    \begin{array}[ccc]                 \\begin\{array\}[ccc]
                             --->
    \end{array}                        \\end\{array\}
  \end{equation}                     \\end\{equation\}

11.Replace initially capitalized words with lowercase ones:

  Search for:   "\#~{$Alpha$}\($Upper$+$Lower$\)~{$Alpha$}"
  Replace with: "\L0"

IMPORTANT:This replacement must be performed withCase sensitiveoption enabled in order to distinguish between lower and uppercase characters.

12.Select a LaTeX \begin{*}..\end{*} environment:

  Search for:   "\\begin\{*\}@{~{{@ }\\end\{*\}}{*}>}{*\\end\{*\}}"

However,this search will not yield the required results with nested environments. By using tagged expressions this problem can be addressed as well:

  "\\begin{\(\{*\}\)}@{~{*\\end\0}{*}>}{*\\end\0}"
           ^^ tag ^^           ^label         ^label

The wildcard"*"is limited to the text within one line. The above RegEx indicates how to use"@{~expression}expression"to overcome this limitation. However,using"**"significantly simplifies the above example:

  Search for:   "\\begin{\(\{*\}\)}**\\end\0}"

will do just fine.

The optionsRelaxed spacingandInline expressions onlyare ignored with regular expressions,as you have different means to specify such preferences.

For example,RegEx"John{@ @>@ }Smith"matches all three instances of the string:

  1. JohnSmith

  2. John    Smith

  3. John
       Smith

Tex中的正则表达式替换转的更多相关文章

  1. HTML5数字输入仅接受整数的实现代码

    这篇文章主要介绍了HTML5数字输入仅接受整数的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. 移动端html5模拟长按事件的实现方法

    这篇文章主要介绍了移动端html5模拟长按事件的实现方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  3. ios – 在applicationWillEnterForeground触发时更改UIView

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  4. UILabel导致应用程序在添加到视图时崩溃(仅限Xcode 6和iOS 8)

    我已将我的项目转换为Xcode6,以便为iOS8构建.但是,特定的UILabel会导致应用程序在添加到视图层次结构时崩溃.这是我收到的唯一错误日志:我无法在项目中的任何位置找到contentInsetsFromFonts方法.此外,我甚至无法在网上任何地方找到它的参考,包括Apple的文档.我没有为这个UIViewController使用NIB,因此UI是在loadView方法中构建的:该应用程序

  5. iOS 7通知中心与标签一样

    您可以试试,也许在将模糊应用到标签之前为模糊添加一点白色.

  6. ios – 使用大写符号在字符串swift中获取URL的正则表达式

    我尝试在文本中获取URL.所以,在此之前,我使用了这样一个表达式:但是当用户输入带有大写符号的URL时(例如Http://Google.com,它与它不匹配)我遇到了问题.我试过了:但什么都没发生.解决方法您可以使用正则表达式中的i内联标志关闭区分大小写,有关可用正则表达式功能的详细信息,请参阅FoundationFrameworkReference.(?ismwx-ismwx)Flagsetti

  7. iOS safari输入插入颜色

    我在iPhone设备上有一个小的CSS问题.我的搜索输入为蓝色,当用户关注它时,插入符号几乎不可见:在所有桌面浏览器中,即使在桌面Safari上,它也具有正确的颜色(白色).知道如何修复此问题并更改iOS设备上的插入颜色吗?

  8. 如何计算iOS 7中的实际字体大小(不是边框)?

    编辑:链接的“重复”问题仅涉及计算文本矩形.我需要在标签缩放之后计算实际字体大小,而不是字符串大小.此方法现已弃用:如何在iOS7中计算UILabel的字体大小,以缩小文字大小以适应?

  9. ios – 如何在Swift 3中使用正则表达式?

    解决方法我相信.当没有其他选项适用时,将使用.allZeros.因此,使用Swift3,您可以传递一个空的选项列表或省略options参数,因为它默认为无选项:要么请注意,在Swift3中,您不再使用error参数.它现在抛出.

  10. ios – 为什么在presentmodalviewcontroller调用时,navigationItem.titleView会左对齐?

    我正在使用UILabel作为导航栏的titleView.它工作正常,除了当我呈现模态视图控制器时,titleView从导航栏的中心移动到最左边.我在3.0及以上测试过.这是相关代码:截图:知道为什么会这样吗?

随机推荐

  1. 法国电话号码的正则表达式

    我正在尝试实施一个正则表达式,允许我检查一个号码是否是一个有效的法国电话号码.一定是这样的:要么:这是我实施的但是错了……

  2. 正则表达式 – perl分裂奇怪的行为

    PSperl是5.18.0问题是量词*允许零空间,你必须使用,这意味着1或更多.请注意,F和O之间的空间正好为零.

  3. 正则表达式 – 正则表达式大于和小于

    我想匹配以下任何一个字符:或=或=.这个似乎不起作用:[/]试试这个:它匹配可选地后跟=,或者只是=自身.

  4. 如何使用正则表达式用空格替换字符之间的短划线

    我想用正则表达式替换出现在带空格的字母之间的短划线.例如,用abcd替换ab-cd以下匹配字符–字符序列,但也替换字符[即ab-cd导致d,而不是abcd,因为我希望]我如何适应以上只能取代–部分?

  5. 正则表达式 – /bb | [^ b] {2} /它是如何工作的?

    有人可以解释一下吗?我在t-shirt上看到了这个:它似乎在说:“成为或不成为”怎么样?我好像没找到’e’?

  6. 正则表达式 – 在Scala中验证电子邮件一行

    在我的代码中添加简单的电子邮件验证,我创建了以下函数:这将传递像bob@testmymail.com这样的电子邮件和bobtestmymail.com之类的失败邮件,但是带有空格字符的邮件会漏掉,就像bob@testmymail也会返回true.我可能在这里很傻……当我测试你的正则表达式并且它正在捕捉简单的电子邮件时,我检查了你的代码并看到你正在使用findFirstIn.我相信这是你的问题.findFirstIn将跳转所有空格,直到它匹配字符串中任何位置的某个序列.我相信在你的情况下,最好使用unapp

  7. 正则表达式对小字符串的暴力

    在测试小字符串时,使用正则表达式会带来性能上的好处,还是会强制它们更快?不会通过检查给定字符串的字符是否在指定范围内比使用正则表达式更快来强制它们吗?

  8. 正则表达式 – 为什么`stoutest`不是有效的正则表达式?

    isthedelimiter,thenthematch-only-onceruleof?PATTERN?

  9. 正则表达式 – 替换..与.在R

    我怎样才能替换..我尝试过类似的东西:但它并不像我希望的那样有效.尝试添加fixed=T.

  10. 正则表达式 – 如何在字符串中的特定位置添加字符?

    我正在使用记事本,并希望使用正则表达式替换在字符串中的特定位置插入一个字符.例如,在每行的第6位插入一个逗号是什么意思?如果要在第六个字符后添加字符,请使用搜索和更换从技术上讲,这将用MatchGroup1替换每行的前6个字符,后跟逗号.

返回
顶部