简体   繁体   English

Eclipse使用正则表达式查找/替换整行

[英]Eclipse Find/Replace Whole Line With Regex

I'm using Eclipse's Find/Replace to format some sql code as a Java string. 我正在使用Eclipse的“查找/替换”将一些sql代码格式化为Java字符串。 I want to place each line of the sql query between quotation marks and add a line break at the end. 我想将sql查询的每一行放在引号之间,并在末尾添加一个换行符。

Here is what I put in the find field: 这是我在查找字段中输入的内容:

(.*)

This is what I put in the replace field 这是我在替换字段中输入的内容

\t\t+ "\1\\n"

Here's simple example (my actual sql query is about 200 lines) 这是简单的示例(我的实际sql查询约为200行)

SELECT *

FROM User
WHERE User.Id = 1232164

And this is what I expect to see after 这就是我期望看到的

    + "SELECT *\n"
    + "\n"
    + "FROM User\n"
    + "WHERE User.Id = 1232164\n"

However, the Find fails when it encounters the blank line it says there are no more matching results and terminates (or jumps to the top of the file if the 'Wrapped Search' option is marked) 但是,当查找遇到空白行时,查找失败,并说没有更多匹配结果,并终止(如果标记了“包装的搜索”选项,则跳转到文件顶部)

I also tried using the following in the find regex 我也尝试在查找正则表达式中使用以下内容

^(.*)
^(.*)$

with the same results 结果相同

Anyone know what I'm doing wrong or is this perhapse a bug in Eclipse. 任何人都知道我在做什么错,或者这也许是Eclipse中的错误。

For what its worth, it works fine in Emacs as I originally wrote it. 就其价值而言,它在我最初编写的Emacs中可以正常工作。

It strikes me as a bug in Eclipse. 它使我成为Eclipse中的错误。 Nevertheless, I've achieved your desired result (in Eclipse Juno) by using a search pattern: 尽管如此,通过使用搜索模式,我已经达到了您想要的结果(在Eclipse Juno中):

(.*)\R

and a replacement string: 和一个替换字符串:

\t\t+ "\1\\n"\R

Note that \\R is a special Eclipse pattern that matches any form of line delimiter, when searching. 请注意, \\R是一种特殊的Eclipse模式,在搜索时可以匹配任何形式的行定界符。 When replacing, it inserts the default line delimiter for the document. 替换时,它将插入文档的默认行定界符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM