加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 在使用VIM进行搜索和替换时,如何保留分组文本并在

发布时间:2020-12-14 06:04:05 所属栏目:百科 来源:网络整理
导读:在VIM(编辑MacVim快照64)中,我正在寻找一种转换这些文本行的方法 can_check_steps = 1can_edit_work_type = 1can_edit_vendor = 1can_edit_place = 1 至 'can_check_steps': can_check_steps,'can_edit_work_type': can_edit_work_type,'can_edit_vendor': c
在VIM(编辑MacVim快照64)中,我正在寻找一种转换这些文本行的方法

can_check_steps = 1
can_edit_work_type = 1
can_edit_vendor = 1
can_edit_place = 1

'can_check_steps': can_check_steps,'can_edit_work_type': can_edit_work_type,'can_edit_vendor': can_edit_vendor,'can_edit_place': can_edit_place,

我知道如何添加前导单引号并将=转换为:使用多行字符替换(我从this question学到的东西)和基本搜索和替换.

但我无法弄清楚如何重复每行的变量名称.我也开始认为应该有一种方法可以在一个命令中使用类似的分组表达式(在正则表达式中使用(和)“捕获”)来完成所有这些操作.

我知道如何使用代码行在Python中执行此操作,但我真的不知道如何使用s / search_for_this / replace_that / options格式在VIM中执行此操作.

编辑:

看完答案后,为什么这不起作用?

^s*(w+) = 1s*$/'1': 1,/g

对于@Jonathan Leffler,:设置所有MacVim产品

--- Options ---
  aleph=224           bufhidden=          cscopequickfix=   noerrorbells          fsync               includeexpr=      nomacmeta             omnifunc=           report=2            showmatch           tabline=          nottimeout          nowildignorecase
  antialias           buflisted         nocscoperelative      esckeys           nofullscreen          incsearch           magic               operatorfunc=     norevins              showmode            tabpagemax=10       ttimeoutlen=-1      wildmenu
noarabic              buftype=          nocscopetag           eventignore=      nogdefault            indentexpr=         makeef=           nopaste             norightleft           showtabline=1       tabstop=4           ttybuiltin          wildmode=full
  arabicshape         cdpath=,cscopetagorder=0    expandtab           guifont=          noinfercase           makeprg=make        pastetoggle=        ruler               sidescroll=0        tagbsearch        nottyfast             wildoptions=
noallowrevins         cedit=^F          nocscopeverbose     noexrc                guifontwide=      noinsertmode          matchtime=5         patchexpr=          rulerformat=        sidescrolloff=0     taglength=0         ttymouse=           window=83
noaltkeymap           charconvert=      nocursorbind          fileencoding=       guipty              isprint=@,161-255   maxcombine=2        patchmode=          scroll=41           smartcase           tagrelative         ttyscroll=999       winheight=1
  ambiwidth=single  nocindent           nocursorcolumn        fileformat=unix     guitablabel=%M%t    joinspaces          maxfuncdepth=100  nopreserveindent    noscrollbind          smartindent         tags=./tags,tags    undodir=.         nowinfixheight
  autochdir           cinoptions=         cursorline          filetype=           guitabtooltip=      key=                maxmapdepth=1000    previewheight=12    scrolljump=1        smarttab            tagstack          noundofile          nowinfixwidth
  autoindent          cmdheight=1         debug=            nofkmap               helpheight=20       keymap=             maxmem=751782     nopreviewwindow       scrolloff=0         softtabstop=0       term=builtin_gui    undolevels=1000     winminheight=1
noautoread            cmdwinheight=7    nodelcombine          foldclose=          helplang=en         keymodel=           maxmemtot=751782    printdevice=      nosecure            nospell             notermbidi            undoreload=10000    winminwidth=1
noautowrite           colorcolumn=        dictionary=         foldcolumn=0      nohidden              keywordprg=man -s   menuitems=25        printencoding=      selectmode=         spellfile=        noterse               updatecount=200     winwidth=20
noautowriteall        columns=269       nodiff                foldenable          history=1000        langmap=            modeline            printfont=courier   shell=/bin/bash     spelllang=en        textauto            updatetime=4000     wrap
  background=dark   nocompatible          diffexpr=           foldexpr=0        nohkmap               langmenu=none       modelines=5         printmbcharset=     shellcmdflag=-c     spellsuggest=best notextmode            verbose=0           wrapmargin=0
nobackup              concealcursor=      diffopt=filler      foldignore=#      nohkmapp              laststatus=2        modifiable          printmbfont=        shellquote=       nosplitbelow          textwidth=0         verbosefile=        wrapscan
  backupcopy=auto     conceallevel=0    nodigraph             foldlevel=0         hlsearch          nolazyredraw        nomodified            printoptions=       shelltemp         nosplitright          thesaurus=          virtualedit=        write
  backupext=~         completefunc=       display=            foldlevelstart=-1   icon              nolinebreak           more                prompt              shellxquote=        startofline       notildeop           novisualbell        nowriteany
  balloondelay=600  noconfirm             eadirection=both    foldmethod=manual   iconstring=         lines=84            mouse=a             pumheight=0       noshiftround          suffixesadd=        timeout             warn                writebackup
noballooneval       nocopyindent        noedcompatible        foldminlines=1    noignorecase          linespace=0       nomousefocus          quoteescape=       shiftwidth=4        swapfile            timeoutlen=1000   noweirdinvert         writedelay=0
  balloonexpr=        cpoptions=aABceFs   encoding=utf-8      foldnestmax=20    noimcmdline         nolisp                mousehide         noreadonly          noshortname           swapsync=fsync      title               whichwrap=b,s
nobinary              cryptmethod=zip     endofline           formatexpr=         imdisable         nolist                mousetime=500       redrawtime=2000     showbreak=          switchbuf=          titlelen=85         wildchar=<Tab>
nobomb                cscopepathcomp=0    equalalways         formatoptions=tcq   iminsert=2          listchars=eol:$    number            norelativenumber      showcmd             synmaxcol=3000      titlestring=        wildcharm=0
  browsedir=last      cscopeprg=cscope    equalprg=           formatprg=          imsearch=2          loadplugins         numberwidth=4       remap             noshowfulltag         syntax=             transparency=0      wildignore=
  backspace=indent,eol,start
  backupdir=.,~/tmp,~/
  backupskip=/tmp/*,/var/folders/70/f8c54mjn4vg_wztz9bd17lp40000gn/T/*
  breakat= ^I!@*-+;:,./?
  casemap=internal,keepascii
  cinkeys=0{,0},0),:,0#,!^F,o,O,e
  cinwords=if,else,while,do,for,switch
  clipboard=autoselect
  comments=s1:/*,mb:*,ex:*/,://,b:#,:%,:XCOMM,n:>,fb:-
  commentstring=/*%s*/
  complete=.,w,b,u,t,i
  completeopt=menu,preview
  define=^s*#s*define
  directory=.,/var/tmp,/tmp
  errorfile=errors.err
  errorformat=%*[^"]"%f"%*D%l: %m,"%f"%*D%l: %m,%-G%f:%l: (Each undeclared identifier is reported only once,%-G%f:%l: for each function it appears in.),%-GIn file included from %f:%l:%c:,%-GIn file included from %f:%l:%c,%-GIn file included from %f:%l:%c,%-GIn file
 included from %f:%l,%-G%*[ ]from %f:%l:%c,%-G%*[ ]from %f:%l:,%-G%*[ ]from %f:%l,%-G%*[ ]from %f:%l,%f:%l:%c:%m,%f(%l):%m,%f:%l:%m,"%f",line %l%*D%c%*[^ ] %m,%D%*a[%*d]: Entering directory `%f',%X%*a[%*d]: Leaving directory `%f',%D%*a: Entering directory `%f
',%X%*a: Leaving directory `%f',%DMaking %*a in %f,%f|%l| %m
  fileencodings=ucs-bom,utf-8,default,latin1
  fileformats=unix,dos
  fillchars=vert:|,fold:-
  foldmarker={{{,}}}
  foldopen=block,hor,mark,percent,quickfix,search,tag,undo
  foldtext=foldtext()
  formatlistpat=^s*d+[]:.)}t ]s*
  fuoptions=maxvert,maxhorz
  grepformat=%f:%l:%m,%f:%l%m,%f  %l%m
  grepprg=grep -n $* /dev/null
  guicursor=n-v-c:block-Cursor/lCursor,ve:ver35-Cursor,o:hor50-Cursor,i-ci:ver25-Cursor/lCursor,r-cr:hor20-Cursor/lCursor,sm:block-Cursor-blinkwait175-blinkoff150-blinkon175
  guioptions=egmrLtT
  helpfile=/Applications/MacVim.app/Contents/Resources/vim/runtime/doc/help.txt
  highlight=8:SpecialKey,@:NonText,d:Directory,e:ErrorMsg,i:IncSearch,l:Search,m:MoreMsg,M:ModeMsg,n:LineNr,r:Question,s:StatusLine,S:StatusLineNC,c:VertSplit,t:Title,v:Visual,V:VisualNOS,w:WarningMsg,W:WildMenu,f:Folded,F:FoldColumn,A:DiffAdd,C:DiffChange,D:DiffDelete,T:DiffText,>:SignColumn,-:Conceal,B:SpellBad,P:SpellCap,R:SpellRare,L:SpellLocal,+:Pmenu,=:PmenuSel,x:PmenuSbar,X:PmenuThumb,*:TabLine,#:TabLineSel,_:TabLineFill,!:CursorColumn,.:CursorLine,o:ColorColumn
  include=^s*#s*include
  indentkeys=0{,e
  isfname=@,48-57,/,.,-,_,+,#,$,%,~,=
  isident=@,192-255
  iskeyword=@,192-255
  lispwords=defun,define,defmacro,set!,lambda,if,case,let,flet,let*,letrec,do*,define-syntax,let-syntax,letrec-syntax,destructuring-bind,defpackage,defparameter,defstruct,deftype,defvar,do-all-symbols,do-external-symbols,do-symbols,dolist,dotimes,ecase,etypecase,eva
l-when,labels,macrolet,multiple-value-bind,multiple-value-call,multiple-value-prog1,multiple-value-setq,prog1,progv,typecase,unless,unwind-protect,when,with-input-from-string,with-open-file,with-open-stream,with-output-to-string,with-package-iterator,define-condition,h
andler-bind,handler-case,restart-bind,restart-case,with-simple-restart,store-value,use-value,muffle-warning,abort,continue,with-slots,with-slots*,with-accessors,with-accessors*,defclass,defmethod,print-unreadable-object
  matchpairs=(:),{:},[:]
  maxmempattern=1000
  mkspellmem=460000,2000,500
  mousemodel=popup_setpos
  mouseshape=i-r:beam,s:updown,sd:udsizing,vs:leftright,vd:lrsizing,m:no,ml:up-arrow,v:rightup-arrow
  nrformats=octal,hex
  paragraphs=IPLPPPQPP TPHPLIPpLpItpplpipbp
  path=.,/usr/include,printexpr=system('open -a Preview '.v:fname_in) + v:shell_error
  printheader=%<%f%h%m%=Page %N
  rightleftcmd=search
  runtimepath=~/.vim,/Applications/MacVim.app/Contents/Resources/vim/vimfiles,/Applications/MacVim.app/Contents/Resources/vim/runtime,/Applications/MacVim.app/Contents/Resources/vim/vimfiles/after,~/.vim/after
  scrollopt=ver,jump
  sections=SHNHH HUnhsh
  selection=inclusive
  sessionoptions=blank,buffers,curdir,folds,help,options,tabpages,winsize
  shellpipe=2>&1| tee
  shellredir=>%s 2>&1
  shortmess=filnxtToO
  spellcapcheck=[.?!]_[])'"^I ]+
  statusline=%F%m%r%h%w[%L][%{&ff}]%y[%p%%][%04l,%04v]
  suffixes=.bak,.o,.h,.info,.swp,.obj
  termencoding=utf-8
  titleold=Thanks for flying Vim
  toolbar=icons,tooltips
  toolbariconsize=small
  ttytype=builtin_gui
  viewdir=~/.vim/view
  viewoptions=folds,cursor
  viminfo='100,<50,s10,h

解决方法

:s/^([^ ]*) .*$/'1': 1,/

匹配部分从行的开头开始,捕获一系列非空白,然后是未捕获的空白和其他任何内容.替换部分以引号,您记住的内容,引号,冒号,空白,您再次记住的内容以及逗号开头.

vim中的分歧行为

Weird,backslashing ( and ) doesn’t even match anything in my VIM,which is MacVim snapshot 64 (although it still says it’s 7.3.390 by Bram Moolenar). Using the v in @Tassos’s answer seems to work.

在Mac OS X 10.7.4上,我使用来自/usr/bin的vim,它将自己标识为:

VIM - Vi IMproved 7.3 (2010 Aug 15,compiled Jun 24 2011 20:00:09)
Compiled by root@apple.com
Normal version without GUI.  Features included (+) or not (-):
-arabic +autocmd -balloon_eval -browse +builtin_terms +byte_offset +cindent 
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments 
-conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs 
-dnd -ebcdic -emacs_tags +eval +ex_extra +extra_search -farsi +file_in_path 
+find_in_path +float +folding -footer +fork() -gettext -hangul_input +iconv 
+insert_expand +jumplist -keymap -langmap +libcall +linebreak +lispindent 
+listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape 
-mouse_dec -mouse_gpm -mouse_jsbterm -mouse_netterm -mouse_sysmouse 
+mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -osfiletype 
+path_extra -perl +persistent_undo +postscript +printer -profile -python 
-python3 +quickfix +reltime -rightleft -ruby +scrollbind +signs +smartindent 
-sniff +startuptime +statusline -sun_workshop +syntax +tag_binary 
+tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title
 -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo 
+vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp
 -xterm_clipboard -xterm_save 
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -D_FORTIFY_SOURCE=0 -Iproto -DHAVE_CONFIG_H -arch i386 -arch x86_64 -g -Os -pipe
Linking: gcc -arch i386 -arch x86_64 -o vim -lncurses

我也在Linux机器上使用vim,它的行为与w.r.t正则表达式相同.其中一个版本标识自己,部分原因是:

VIM - Vi IMproved 7.0 (2006 May 7,compiled Nov  5 2008 11:07:46)
Included patches: 1,3-4,7-9,11,13-17,19-26,29-31,34-44,47,50-56,58-64,66-73,75,77-92,94-107,109,202,234-235,237
Modified by <bugzilla@redhat.com>
Compiled by <bugzilla@redhat.com>
Huge version without GUI.

我不确定是否有值得展示的东西:全部设置.这是Mac版Vim的输出:

ambiwidth=single    foldclose=          omnifunc=           syntax=
noautochdir           foldcolumn=0        operatorfunc=       tabline=
  autoindent          foldenable        nopaste               tabpagemax=10
  autoprint           foldexpr=0          pastetoggle=        tabstop=4
noautoread            foldignore=#        patchexpr=          tagbsearch
noautowrite           foldlevel=0         patchmode=          taglength=0
noautowriteall        foldlevelstart=-1 nopreserveindent      tagrelative
  background=light    foldmethod=manual   previewheight=12    tags=./tags,tags
  backspace=2         foldminlines=1    nopreviewwindow       tagstack
nobackup              foldnestmax=20      printdevice=        term=xterm-color
  backupcopy=auto     formatexpr=         printencoding=      termencoding=
  backupext=~         formatoptions=tcq   printfont=courier noterse
nobeautify            formatprg=          printmbcharset=     textauto
nobinary              fsync               printmbfont=      notextmode
nobomb              nogdefault            printoptions=       textwidth=0
  bufhidden=          helpheight=20       prompt              thesaurus=
  buflisted           helplang=en         pumheight=0       notildeop
  buftype=          nohidden              quoteescape=       timeout
  cedit=^F            history=20        noreadonly            timeoutlen=1000
  charconvert=      nohlsearch            redrawtime=2000   notitle
nocindent           noicon              norelativenumber      titlelen=85
  cinoptions=         iconstring=         remap               titlestring=
  cmdheight=1       noignorecase          report=2          nottimeout
  cmdwinheight=7      iminsert=0        noruler               ttimeoutlen=-1
  colorcolumn=        imsearch=0          rulerformat=        ttybuiltin
  columns=80          includeexpr=        scroll=32           ttyfast
nocompatible        noincsearch         noscrollbind          ttymouse=xterm
  completefunc=       indentexpr=         scrolljump=1        ttyscroll=999
noconfirm           noinfercase           scrolloff=0         undodir=.
nocopyindent        noinsertmode        nosecure            noundofile
  cpoptions=aABceFs   isprint=@,161-255   selectmode=         undolevels=1000
  cryptmethod=zip     joinspaces          shell=/bin/sh       undoreload=10000
  cscopepathcomp=0    key=                shellcmdflag=-c     updatecount=200
  cscopeprg=cscope    keymodel=           shellquote=         updatetime=4000
  cscopequickfix=     keywordprg=man -s   shelltemp           verbose=0
nocscopetag           langmenu=           shellxquote=        verbosefile=
  cscopetagorder=0    laststatus=1      noshiftround          virtualedit=
nocscopeverbose     nolazyredraw          shiftwidth=4      novisualbell
nocursorbind        nolinebreak         noshortname           warn
nocursorcolumn        lines=66            showbreak=        noweirdinvert
nocursorline        nolisp              noshowcmd             whichwrap=b,s
  debug=            nolist              noshowfulltag         wildchar=<Tab>
nodelcombine          listchars=eol:$    showmatch           wildcharm=0
  dictionary=         loadplugins         showmode            wildignore=
nodiff                magic               showtabline=1     nowildmenu
  diffexpr=           makeef=             sidescroll=0        wildmode=full
  diffopt=filler      makeprg=make        sidescrolloff=0     wildoptions=
nodigraph             matchtime=5       nosmartcase           window=0
  directory=/tmp      maxcombine=2      nosmartindent         winheight=1
  display=            maxfuncdepth=100  nosmarttab          nowinfixheight
  eadirection=both    maxmapdepth=1000    softtabstop=0     nowinfixwidth
noedcompatible        maxmem=450298     nospell               winminheight=1
  encoding=utf-8      maxmemtot=450298    spellfile=          winminwidth=1
  endofline           menuitems=25        spelllang=en        winwidth=20
  equalalways         modeline            spellsuggest=best   wrap
  equalprg=           modelines=0       nosplitbelow          wrapmargin=0
noerrorbells          modifiable        nosplitright          wrapscan
  esckeys           nomodified            startofline         write
  eventignore=        more                statusline=       nowriteany
  expandtab           mouse=              suffixesadd=        writebackup
noexrc                mousemodel=extend   swapfile            writedelay=0
  fileencoding=       mousetime=500       swapsync=fsync
  fileformat=unix     number              switchbuf=
  filetype=           numberwidth=4       synmaxcol=3000

您描述的行为听起来好像正在使用PCRE(Perl兼容的正则表达式).也许配置中的-perl与那里相关.我没有在显示的设置中看到任何可能改变正则表达式模式的内容.也许你可以运行你的MacVim并显示macvim –version的输出,然后输出:set all.哦,drat,你没有足够的权限将它们编辑到这条消息中.我发现一个80柱的窗口对于:设置所有信息和粘贴到SO是合理的;我通常使用120个柱窗.

请在我的个人资料中找到我的电子邮件并将数据发送给我.我很想知道MacVim设置有什么不同(如果只是这样我可以将它们设置回’正常’,如果我可以使用它).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读