记事本++正则表达式中的换行符

发布于 2024-10-06 11:54:55 字数 161 浏览 6 评论 0原文

假设您有以下文件:

x
a
b
c
x
x
a
b
c
x
x

并且您想要使用 Notepad++ 查找序列 abc(并选择整 3 行)。请问如何在正则表达式中表达换行符?

Suppose you have this file:

x
a
b
c
x
x
a
b
c
x
x

and you want to find the sequence abc (and select the whole 3 lines) with Notepad++ . How to express the newline in regex, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

携余温的黄昏 2024-10-13 11:54:56

我找到了解决这个问题的方法。
简单地说,在扩展模式下,将所有 \r\n 替换为文档其余部分中不存在的字符串,例如。 ,,,newline,,,(注意特殊的正则表达式字符,例如 $&* )。
然后切换到正则表达式模式,进行一些替换(现在换行符是 ,,,newline,,,)。
接下来,再次切换到扩展模式,并将所有 ,,,newline,,, 替换为 \r\n

I found a workaround for this.
Simply, in Extended mode replace all \r\n to a string that didn't exist in the rest of the document eg. ,,,newline,,, (watch out for special regexp chars like $, &, and *).
Then switch to Regexp mode, do some replacements (now newline is ,,,newline,,,).
Next, switch to Extended mode again and replace all ,,,newline,,, to \r\n.

空气里的味道 2024-10-13 11:54:56

对于记事本 6 及更高版本,请将此作为正则表达式执行:

  • 选择“搜索模式”>“正则表达式(w/o . 匹配换行符)
  • 并在“查找内容”文本框中:a[\r\n]b[\r\n]+c[\r\n]

或者如果您是查看(Windows 或 Unix)文件以查看其换行符为 \r\n\n 那么您可能会发现使用扩展模式更容易:

  • 选择搜索模式>扩展 (\n、\r、\t、\0、\x...)
  • 在 Windows 的“查找内容”文本框中:a\r\nb\r\nc\r\n
  • 或者在 Unix 的查找内容文本框中: a\nb\nc\n

不清楚 OP 意图是否也是选择尾随行返回(在“c”之后),就像这样需要删除线路。

要不选择尾随行返回(适合用非空字符串替换),只需从匹配语句中删除最后一行返回即可。

请注意,如果字符串的最后一行应该有匹配项,但没有匹配的尾随行返回,则匹配失败。

For Notepad 6 and beyond, do this as a regular expression:

  • Select Search Mode > Regular expression (w/o . matches newline)
  • And in the Find what Textbox : a[\r\n]b[\r\n]+c[\r\n]

or if you are looking at the (Windows or Unix) file to see its line breaks as \r\n or \n then you may find it easier to use Extended Mode:

  • Select Search Mode > Extended (\n, \r, \t, \0, \x...)
  • And in the Find what Textbox for Windows: a\r\nb\r\nc\r\n
  • Or in the Find what Textbox for Unix: a\nb\nc\n

Wasn't clear if the OP intent is to select the trailing line return (after the 'c') as well, as would be necessary to remove the lines.

To not select the trailing line return, as appropriate for replacing with a non-empty string, simply remove the final line return from the matching statement.

Note that if there should be a match on the last line of the string, without a matching trailing line return, the match fails.

落在眉间の轻吻 2024-10-13 11:54:56

当文档是 Windows CR/LF 时,我遇到了这个小问题,

如果您单击 .要匹配换行符,您需要 .. 匹配 CR/LF,因此如果有,

<blah><blah>",
"<more><blah>

您需要使用 ",.." 来匹配某些字符串逗号 cr/lf 另一个字符串

I have run accross this little issue when the document is windows CR/LF

If you click the box for . to match newlines you need .. to match CR/LF so if you have

<blah><blah>",
"<more><blah>

you need to use ",.." to match some string comma cr/lf another string

秋意浓 2024-10-13 11:54:56

a\r\nb\r\nc 适用于我,但不适用于 ^a\x0D\x0Ab\x0D\x0Ac

嗯,太糟糕了,换行符不能与常规一起使用表达式。现在我必须再次回到 Textpad。 :(

a\r\nb\r\nc works for me, but not ^a\x0D\x0Ab\x0D\x0Ac

Hmm, too bad that newline is not working with regular expressions. Now I have to go back to Textpad again. :(

秋心╮凉 2024-10-13 11:54:56

选择搜索模式
Extended (\n, \r, \t, \0, \x...)

\n 是新行,这样
我是曼努埃尔

Select Search Mode Which is
Extended (\n, \r, \t, \0, \x...)

\n is new line and such
This is Manuel

于我来说 2024-10-13 11:54:56

查找:“(^a.$)\r\n(b.)\r\n^(c.*)$” - 拾取 3 整行,仅存储数据

替换为:“\ 1\2\3" - 放下(重播)数据

在带有 Notepad++ v7.9.5 的正则表达式中工作正常

占位符: ^ 行的开始和 $ 行结束可以在 ()store 内部或外部,如图所示,但在给定中显然没有必要例子。注意“[^x]”是不同的 - 这里“^”是“NOT”。

存储和重播的优点允许更复杂的模式匹配,而无需再次输入您想要的结果,甚至可以更改重播:“\2\3\1”代表“bca”

Find: "(^a.$)\r\n(b.)\r\n^(c.*)$" - pickup 3 whole lines, only storing data

Replace with: "\1\2\3" - Put down (replay) data

Works fine in Regex with Notepad++ v7.9.5

Place holders: ^ Start and $ End of line can be inside or out of ()store as shown, though clearly not necessary in given example. Note "[^x]" is different - here "^" is "NOT".

Advantage of storing and replay allows much more complicated pattern match without having to type in again what you want to end up with, and even change of replay: "\2\3\1" for "bca"

浅唱々樱花落 2024-10-13 11:54:56

在 Notepad++ 中,您还可以尝试突出显示文本的所需部分,然后按 CTRL+J。

这将使文本合理,从而删除所有行结尾。

In Notepad++ you can also try highlighting the desired part of the text and then pressing CTRL+J.

That would justify the text and thus removing all line endings.

千と千尋 2024-10-13 11:54:55

Notepad++ 可以轻松地做到这一点,您甚至不需要正则表达式

在查找对话框中查看左下角并将搜索模式切换为扩展,这允许 \n 等

。 '将查找 \r\n (回车符、换行符)

a\r\nb\r\nc

将找到三行以上的模式

Notepad++ can do that comfortably, you don't even need regexes

In the find dialogue box look in the bottom left and switch your search mode to Extended which allows \n etc.

As odds on you're working on a file in windows format you'll be looking for \r\n (carriage return, newline)

a\r\nb\r\nc

Will find the pattern over three lines

旧时光的容颜 2024-10-13 11:54:55

2012 年 6 月 18 日更新

使用新的 Notepad++ v6,您可以确实可以使用正则表达式搜索换行符。所以你可以使用

a\r\nb\r\nc

Even 与正则表达式来完成你想要的。注意 \r\n 是换行符的 Windows 编码。在 Unix 文件中,它只是 \n

不幸的是,当使用正则表达式搜索时,您无法在 Notepad++ 中执行此操作。 Notepad++ 基于 Scintilla 编辑器组件,该组件不处理正则表达式中的换行符。

您可以使用扩展搜索进行换行搜索,但我认为这不会帮助您搜索 3 行。

更多信息请点击此处

更新:Robb 和 StartClass0830 关于扩展搜索的看法是正确的。它确实有效,但在使用正则表达式搜索时无效。

Update 18th June 2012

With the new Notepad++ v6, you can indeed search for newlines with regexes. So you can just use

a\r\nb\r\nc

even with regular expressions to accomplish what you want. Note \r\n is Windows encoding of line-breaks. In Unix files, its just \n.

Unfortunately, you can't do that in Notepad++ when using regex search. Notepad++ is based on the Scintilla editor component, which doesn't handle newlines in regex.

You can use extended search for newline searching, but I don't think that will help you search for 3 lines.

More info here.

Update: Robb and StartClass0830 were right about extended search. It does work, but not when using regular expressions search.

呆头 2024-10-13 11:54:55
^a\x0D\x0Ab\x0D\x0Ac

这将起作用 \x0D 是换行符,\x0A 是回车符。假设文件中的每一行都以 ascii 10 和 13 结尾。

^a\x0D\x0Ab\x0D\x0Ac

This will work \x0D is newline and \x0A is carriage return. Assumption is that each line in your file ends with ascii 10 and 13.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文