Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

regex - Is there a decent Vim regexp OR command? What is the best way to find mismatched if else's?

I have some mismatching if and fi statements in a script. I would like to strip out everything but the if's else's and fi's. Just so I can see the structure. Why am working so HARD with such a powerful editor? I need a BIGFATOR operator for regexp or some epiphany that has eluded me... I don't care for pontification on regular expressions just something practical working in VIM7.2.

:g/[ ^]if [/print

will print out the ifs

:g/[ ^]fi/print

will printout the fi

What I want to do is or the conditions

:g/[ ^]fi BIGFATOROPERATOR [ ^]fi/print

I have had success doing the following... but I feel I am working TOO HARD!

:call TripMatch('[ ^]*if [', 'else', 'fi[ $]')

function! TripMatch(str1, str2, str3)

let var1 = a:str1

let var2 = a:str2

let var3 = a:str3

let max = line("$")

let n = 1

for n in range (1, max)

let currentline = getline(n)

if currentline =~? var1

   echo n "1:" currentline

else

   if currentline =~? var2

      echo n "2:" currentline

   else

      if currentline =~? var3

         echo n "3:" currentline

      else

         let foo = "do nothing"

      endif

   endif

endif     

endfor

endfunction

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
:g/[ ^]if|[ ^]fi/print

BIGFATOROPERATOR is |. It's called the alternation operator. In PCRE, it's plain |. It's escaped in Vim/ex because | is used elsewhere for general commands (or something -- FIXME).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...