如何在emacs中查找和替换特殊正则表达式

2024-04-26 06:21:26 发布

您现在位置:Python中文网/ 问答频道 /正文

如果有人能告诉我如何在emacs中这样做,这将节省我大量的时间:我想做一个查找和替换,但把我“找到”的东西保存到“替换”中。你知道吗

基本上,我的文件里有一堆打印语句。我重新定义了print to verboseprint(),它在脚本运行时显示verbose标志时打印。你知道吗

但是,由于我使用的是Python2.4,所以调用print语句时没有括号:

print "Hello, world!"

我做了一个查找和替换来得到这个:

verboseprint "Hello, world!"

但是现在,我想做一个查找和替换来得到这个:

verboseprint ("Hello, world!")

这包括以下步骤:

  • 基于正则表达式查找字符串(我知道emacs可以做到)
  • 让替换字符串占用原始字符串的一部分(我不确定emacs是否可以做到这一点)

所以在伪代码中,当我进行“查找”时,我会搜索:

"verboseprint" + (characters until newline)

我会把它换成

"verboseprint(" + (characters until newline) + ")"

这在emacs中是可能的吗?任何帮助都将不胜感激。谢谢!你知道吗


Tags: 文件字符串helloworld定义时间newline语句
2条回答

我想我会继续工作,假设你可以编辑文件,当你回答我的评论时会编辑

不熟悉emacs(或者老实说,太熟悉Python 2.5),但是假设您可以直接访问该文件,那么应该可以:

我的测试文件

VERBOSE=True

def verboseprint(*args,**kw):
    if VERBOSE:
        print *args, **kw

##Base case
verboseprint "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"

if VERBOSE: ## Testing for Indentation
    verboseprint "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

## Test kwargs    
verboseprint "Ut enim ad minim veniam,",end="\n\n"

## Test args
verboseprint "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea","commodo consequat."

## Test Single-line
if VERBOSE: verboseprint "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "

编写替换脚本

import re

FILE= ## Insert File Location
OUTPUT= ## Output File Location, different to be safe

REPLACERE=re.compile("verboseprint (?P<original>[^:]*)")
NEWPRINT="verboseprint( %s )\n"

def replacefunction(match):
    return NEWPRINT % match.group('original').rstrip()

f=open(FILE) ## I don't recall if context managers existed in Python 2.5
out=[]
for line in f.readlines():
    out.append(REPLACERE.sub(replacefunction,line))
f.close()

f=open(OUTPUT,'w')
f.write("".join(out))
f.close()

我的输出文件

VERBOSE=True

def verboseprint(*args,**kw):
    if VERBOSE:
        print *args, **kw

##Base case
verboseprint( "Lorem ipsum dolor sit amet, consectetur adipiscing elit," )

if VERBOSE: ## Testing for Indentation
    verboseprint( "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." )

## Test kwargs    
verboseprint( "Ut enim ad minim veniam,",end="\n\n" )

## Test args
verboseprint( "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea","commodo consequat." )

## Test Single-line
if VERBOSE: verboseprint( "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " )

下面是一个快速的Emacs命令。使用M-x foo或将foo绑定到键。你知道吗

这里的regexp假设从verboseprint到行尾的文本在双引号字符内。如果不是,或者您想在这里或那里允许空白,则相应地调整regexp。你知道吗

(defun foo (beg end)
  "Wrap argument to `verboseprint` in parens.
If the region is active and nonempty then do it throughout just the region.
Otherwise do it throughout the buffer."
  (interactive "r")
  (unless  (and transient-mark-mode  mark-active  (> end beg))
    (setq beg  (point-min)
          end  (point-max)))
  (unless (<= beg end) (setq beg (prog1 end (setq end beg))))
  (goto-char beg)
  (replace-regexp "verboseprint \"\\(.*\\)\"$"
                  "verboseprint (\"\\1\")"
                  nil beg end))

你也可以用C-M-%query-replace-regexp)做同样的事情。在这种情况下,你不能把反斜杠加倍。你知道吗

相关问题 更多 >