简体   繁体   English

vb.net正则表达式与字符串的比较

[英]vb.net regex comparison to string

im trying to copy the lines until an empty line is detected but this code lags my computer i dont know what im doing wrong it is because im running while loop inside another while loop? 我试图复制行,直到检测到空行,但是此代码滞后于我的计算机,我不知道我在做什么错,这是因为我在另一个while循环内运行while循环? here is my code: 这是我的代码:

ElseIf String.Compare(line, "the") = 1 And Not line.ToLower().Contains("by") Then
            While True

                Dim title = New Regex("^\s*$").Matches(line).Count
                If title = 1 Then Exit While

                builder.AppendLine(line)
                builder.AppendLine(reader.ReadLine())

            End While

You're not resetting the line variable. 您没有重置line变量。 Something like this should work probably: 这样的事情可能应该起作用:

While True
    Dim title = New Regex("^\s*$").Matches(line).Count
    If title = 1 Then Exit While
    builder.AppendLine(line)
    line = reader.ReadLine()
End While

EDIT 编辑

Instead of regex you could instead just use String.IsNullOrWhiteSpace() which might make your code easier to read in the future. 可以使用String.IsNullOrWhiteSpace()代替正则表达式,而使将来的代码更易于阅读。

It's hard to tell exactly what you are trying to do, with that short snippet of code, but the reason why it doesn't work is clear. 很难用那段简短的代码来确切地说明您要做什么。但是,它不起作用的原因很明显。 You will get stuck in an infinite loop if title does not equal 1 . 如果title不等于1您将陷入无限循环。 You assume, I think, that the value of line will change reach time you call reader.ReadLine inside the While loop. 我认为,您认为line的值将更改While循环内调用reader.ReadLine到达时间。 You probably meant to do something like this: 您可能打算这样做:

line = reader.ReadLine()
builder.AppendLine(line)

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

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