繁体   English   中英

遍历字符串的算法遇到麻烦

[英]Having trouble with an algorithm that loops through a string

我正在使用Visual Studio社区。
嗨,我写了一个循环遍历一系列彩票图纸的循环,我想抓完整张图纸并剪下日期,图纸和强力球号码。

第一次循环运行一切正常,我得到06/29/2002 04 33 35 36 45 Powerball: 22 ,但是第二次循环获得07/03/2002“ 04 33 35 36 45”强力球: 22。

第二次遍历循环,返回到第一个图形的编号。 我知道我会写

Numbers(i) = Full_History.Substring(Start + 10, 16)Balls(i) = FullHistory.Substring(Start + 26, 14) ,但这似乎不对。 我希望我能解释这个权利。 任何帮助将不胜感激。

Dim Start As Integer = 0                                                'Create a variable for start
Dim Finish As Integer = 40                                              
For i As Integer = 0 To Array_Size - 1 Step 1                           'Loop through the string
Full_Draw(i) = Full_History.Substring(Start, Finish)                'Store the full drawing
Dates(i) = Full_History.Substring(Start, 10)                        'Store the Date of the drawing
Numbers(i) = Full_History.Substring(10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(26, 14)                           'Store the 'ball' if necessary
Start += 40                                                         'Increment the start variable

如果您有这样的专用文本,则可以使用正则表达式提取数据,如以下代码段所示:

Dim rx As New System.Text.RegularExpressions.Regex("(\d\d\/\d\d\/\d{4})\s(\d\d)\s(\d\d)\s(\d\d)\s(\d\d)\s(\d\d)\s+Powerball:\s(\d\d)")
Dim s = "06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22 06/29/2002 04 33 35 36 45  Powerball: 22"
Dim matches = rx.Matches(s)
Dim sb As New System.Text.StringBuilder
For Each m As System.Text.RegularExpressions.Match In matches
    sb.AppendLine("Date: " & m.Groups(1).Value)
    sb.AppendLine("#1: " & m.Groups(2).Value)
    sb.AppendLine("#2: " & m.Groups(3).Value)
    sb.AppendLine("#3: " & m.Groups(4).Value)
    sb.AppendLine("#4: " & m.Groups(5).Value)
    sb.AppendLine("#5: " & m.Groups(6).Value)
    sb.AppendLine("Powerball: " & m.Groups(7).Value)
    sb.AppendLine()
Next
MessageBox.Show(sb.ToString)

尽管它不是有史以来最高级的RegEx,但是它应该很容易遵循:

  • \\ d代表一个数字
  • /是文字斜杠字符
  • {4}是一个量词,表示前一事物的4
  • / s是空格
  • / s +表示尽可能多的空格
  • ()表示捕获组,允许访问每个匹配项的.Groups属性中的值

因此,您可以在给定的文本中找到所有匹配项,并且可以通过对匹配项进行迭代来轻松提取所有子组。


对于您的一般问题:

Numbers(i) = Full_History.Substring(10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(26, 14)   

这些行将始终在源字符串中裁剪相同的部分(例如,索引10到26)。 您还需要使用Start变量来缩放Substring的开始部分,例如

Numbers(i) = Full_History.Substring(Start + 10, 16)                         'Store the numbers drawn
Balls(i) = Full_History.Substring(Start + 26, 14)   

否则,不必惊讶每个元素的结局相同。 为了避免更多的混乱,请先将整个内容裁剪掉,然后将这个新字符串分解成各个部分:

For i As Integer = 0 To Array_Size - 1 Step 1                           'Loop through the string
    Dim ThisDraw As String = Full_History.Substring(Start, Finish)
    Full_Draw(i) = ThisDraw
    'Notice the fixed indizes now
    Dates(i) = ThisDraw.Substring(0, 10)                        'Store the Date of the drawing
    Numbers(i) = ThisDraw.Substring(10, 16)                         'Store the numbers drawn
    Balls(i) = ThisDraw.Substring(26, 14)                           'Store the 'ball' if necessary
    Start += Finish
Next

谢谢你的回复。 我通过在数组中剪切单个图形,然后在其中剪切出一个子字符串(单个图形),而不是试图直接从“ FullString”数组中剪切图形,日期,数字和球,解决了该问题。流。

感谢Jim的所有帮助

暂无
暂无

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

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