简体   繁体   English

在Notepad ++中,如何找到第n个字符串

[英]In Notepad++ how do I find the nth occurrence of a string

I have a huge text file where records are identified by the string MSH. 我有一个巨大的文本文件,其中记录由字符串MSH标识。

I need to find the 200th record. 我需要找到第200条记录。 I am hoping that there is a regular expression I can use in Notepad++ that would enable me to find the 200th occurrence of the string MSH. 我希望有一个正则表达式,我可以在Notepad ++中使用,这将使我能够找到第200次出现的字符串MSH。

If your file is just an enormous one-liner delimited by the string "MSH" you could use this in a regular expression find. 如果您的文件只是由字符串“MSH”分隔的巨大单行,您可以在正则表达式查找中使用它。

But, make sure your cursor is at the beginning of the file or it'll just search for the next 200th record from where you started! 但是,请确保您的光标位于文件的开头,否则它将只搜索您开始的一条第200条记录!

Find: 找:

((.*?)MSH){199} ((。*?)MSH){} 199

This should highlight the first 199 records, so the next unhighlighted record is the 200th. 这应突出显示前199条记录,因此下一条未突出显示的记录是第200条记录。


OR, take it a bit further! 或者,进一步吧!
Again, in a regular expression find replace, again make sure your cursor is at the beginning of the file. 再次,在正则表达式中查找替换,再次确保您的光标位于文件的开头。

Find: 找:

((.*?)MSH){199}((.*?)MSH{1}).* ((。*?)MSH){199}((。*?)MSH {1})。*

Replace: 更换:

$3 $ 3

Should replace the entire contents of the window with just the 200th record. 应该只用第200条记录替换窗口的全部内容。

NB: This assumes that the string "MSH" is not part of any of the records in the file. 注意:这假定字符串“MSH”不是文件中任何记录的一部分。


As a footnote, I strongly doubt any of this is quick over a large file. 作为一个脚注,我强烈怀疑任何这个都是快速的大文件。 Scripting is almost certainly a better option. 脚本几乎肯定是更好的选择。 Or possibly even dropping it into Excel and using text-to-columns. 或者甚至可能将其放入Excel并使用文本到列。

Place cursor after 200-th occurrence of MSH 在第200次出现MSH后放置光标

^(.*?MSH){200}\K

在此输入图像描述

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

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