在空行之后修剪文件

发布于 2024-12-24 22:16:50 字数 658 浏览 4 评论 0原文

我有一个包含多个空行的文本文件,我试图返回其中两个空行之间的所有行,

所以如果我有一个如下所示的文件:

 ____________________________
 1########################
 2##########################
 3
 4########################
 5##########################
 6#######################
 7
 8#########################
 9##########################
 10#######################
 11####################
 12########################
 13#########################
 14
 15##########################
 ----------------------------

我想抓取第 8-13 行。不幸的是,它可能并不总是 8-13,因为它可能是 9-20 或 7-8,但它总是在第二个和第三个断线之间。

我知道如何修剪字符并拉出奇异的线条,但我不知道如何修剪整个部分。

任何帮助将不胜感激,即使您只是向我指出一个教程。

提前致谢。

I have a text file that has multiple blank lines and Im trying to return all the lines between two of them specifically

so if I have a file that looks like this:

 ____________________________
 1########################
 2##########################
 3
 4########################
 5##########################
 6#######################
 7
 8#########################
 9##########################
 10#######################
 11####################
 12########################
 13#########################
 14
 15##########################
 ----------------------------

I would like to grab lines 8-13. Unfortunately, it might not always be 8-13 as it could be 9-20 or 7-8, but it will however always be between the 2nd and 3rd line break.

I know how to trim characters and pull out singular lines, but I have no idea how to trim entire sections.

Any help would be appreciated, even if you just point me to a tutorial.

Thanks in advance.

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

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

发布评论

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

评论(2

落在眉间の轻吻 2024-12-31 22:16:50

这里的基本思想是将整个内容作为一个字符串,在双换行符处将其分成几组,然后引用您想要的组(在您的情况下,是第三个组)。

Dim value As String = File.ReadAllText("C:\test.txt")

Dim breakString As String = Environment.NewLine & Environment.NewLine

Dim groups As String() = value.Split({breakString}, StringSplitOptions.None)

Dim desiredString As String = groups(2)

MsgBox(desiredString)

编辑:
回答您评论中的问题 -

Environment.NewLine 是指定换行符的更动态的方式。假设您在 Windows 上运行 - 您也可以使用 VbCrLf。这个想法是,如果您要在 Linux 上编译相同的代码,Environment.NewLine 将生成一个 Lf。您可以在此处查看更多信息:http://en.wikipedia.org/wiki/Newline

我使用Environment.NewLine & 的原因Environment.NewLine 是因为您想要在有两个换行符的地方中断信息(一个在段落最后一行的末尾,一个在下一段之前的空行)

The basic idea here is to get the entire thing as a string, split it into groups at the double line breaks, and then reference the group you want (in your case, the third one).

Dim value As String = File.ReadAllText("C:\test.txt")

Dim breakString As String = Environment.NewLine & Environment.NewLine

Dim groups As String() = value.Split({breakString}, StringSplitOptions.None)

Dim desiredString As String = groups(2)

MsgBox(desiredString)

Edit:
In response to the question in your comment -

Environment.NewLine is a more dynamic way of specifying a line break. Assuming you're running on windows - you could use VbCrLf as well. The idea is that if you were to compile the same code on Linux, it Environment.NewLine would generate a Lf instead. You can see here for more information: http://en.wikipedia.org/wiki/Newline

The reason I used Environment.NewLine & Environment.NewLine is because you want to break your information where there are two line breaks (one at the end of the last line of a paragraph, and one for the blank line before the next paragraph)

荆棘i 2024-12-31 22:16:50

我最终做的是修剪最后一部分并搜索第一部分中我需要的内容(我知道我没有在问题中包含搜索部分,但我只是想找出一种方法来缩小搜索结果范围它会产生重复的结果)。我发布此内容是为了防止其他人偶然发现此内容并寻求一些答案。

Dim applist() = System.IO.File.ReadAllLines("C:\applist.txt")
Dim findICSName As String = "pid"
Dim ICSName As New Regex("\:.*?\(")
Dim x = 0
    Do Until applist(x).Contains("Total PSS by OOM adjustment:")
        If applist(x).Contains(findICSName) Then
            app = ICSName.Match(applist(x)).Value
            app = app.TrimStart(CChar(": "))
            app = app.TrimEnd(CChar("("))
            ListBox1.Items.Add(app)
        End If
        x = x + 1
    Loop
End If

其工作原理是,它会遍历正则表达式的每一行,直到到达断点“Total PSS by OOM adjustment:”中的第一个单词。

What I ended up doing was trimming the last part and searching for what I needed in the first part (I know I didnt include the searching part in the question, but I was just trying to figure out a way to narrow down the search results as it would have had repeated results). Im posting this incase anyone else stumbles upon this looking for some answers.

Dim applist() = System.IO.File.ReadAllLines("C:\applist.txt")
Dim findICSName As String = "pid"
Dim ICSName As New Regex("\:.*?\(")
Dim x = 0
    Do Until applist(x).Contains("Total PSS by OOM adjustment:")
        If applist(x).Contains(findICSName) Then
            app = ICSName.Match(applist(x)).Value
            app = app.TrimStart(CChar(": "))
            app = app.TrimEnd(CChar("("))
            ListBox1.Items.Add(app)
        End If
        x = x + 1
    Loop
End If

How this works is that it looks through each line for the regex until it reaches first word in the breakpoint "Total PSS by OOM adjustment:"

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