2017-06-27 33 views
2

我正在使用LaTeX文檔。我需要重新格式化我在vim中所有打開的緩衝區中擁有的小節標題。Vim,正則表達式搜索並在所有緩衝區中替換字母大小寫更改

通常,當我需要修改所有緩衝區時,我會在vim中執行一個像bufdo %s/pattern/replacement/g | update這樣的命令。這是我打算再次做的。我需要幫助的是模式和替換字符串。

  • \subsection{word}
  • \subsection{some words}
  • \subsection{some Words CAPitalized weirdlY}
  • \subsection{and some with $\control$ sequences in them}

得到的搜索和替換應該不會影響字符串:

,我需要匹配具有以下格式的字符串如下所示:

  • \subsection{Word}
  • \subsection{Some Words}
  • \subsection{Some Words Capitalized Weirdly}
  • \subsection{And Some With $\control$ Sequences In Them}

到目前爲止,我已經嘗試了搜索字符串:

  1. %s/\\subsection{\(.\)\(\w*\)}/\\subsection{\u\1\L\2}/gc
  2. %s/\\subsection{\v<\(.\)\(\w*\)}/\\subsection{\u\1\L\2}/gc

數量1只匹配單個的詞語,把它們轉化成正確的格式。

2號不起作用。我的目標是將下面鏈接中SO回答中引用的序列與我的需求相結合,但我想我沒有正確使用它。


,我嘗試使用摸不着頭腦的資源:

注:如果我要回去通過,並重新輸入$\control字符如果替換也大寫他們,我會沒事的。

回答

2
bufdo %s/\%(\\subsection{.*\)\@<=\%(.*}\)\@=\<\(\w\)\(\w*\)\>/\u\1\L\2/gc 

這應該利用一切內部\subsection{....}塊,遺憾的是包括數學序列裏面,但它每次都會要求您進行確認。搜索比鏈接的「前一個問題」涉及更多一點:

\%( non-capturing group 
\\...{ literal `\subsection{` 
.*  any number of characters 
\)  end of group 
\@<= zero-width lookbehind 

\%( non-capturing group 
.*  any number of characters 
\)  end of group 
\@=  zero-width lookahead 

\<  zero-width start of word 
\(  start of 1st group 
\w  word letter 
\)  end of group 
\(  start of 2nd group 
\w*  any number of letters 
\)  end of group 
\>  zero-width end of word 
+0

很好。謝謝。 –

相關問題