0

ほとんどが英字のテキスト ファイルを読み込んでいます。内容はあまり関係ありませんが、各行のサイズは非常に重要です。このテキストをフィードするプロセスでは、各行が 50 文字を超えないようにする必要があります。そのため、テキストを前処理し、改行を追加して、それが確実に行われるようにします。

^.*$ のようないくつかの VB.NET 正規表現を試しましたが、実際には 50 文字で行が分割されません。結果を取得し、各一致を反復処理してから、それを切り取り、メモリ内のオブジェクトに書き込みます。これは単一の正規表現パスで実行できますか?

それ以外の場合は、ストリームリーダーを使用し、各行で長さを確認し、<=50 の場合はストリームライターで書き出します。50 を超える場合は、50 のセクションに分割してから、ストリームライターを使用します。

私のテキストの簡単な例:

 119  SMITH KATY AAAA  F   ZZZ     X NB SX ET
          MILES,200/LM450                       
 120  JONES  THOMAS      W   QQQ  66-W NB OS SC LW EP       
                                              ET
          L/G/B/MAY20-2010/JONES/THOMAS/KEITH      121  BUBBA BILLY  HH4  S   XQT 2PA-F  1 IP SC LH ET
                                              DOCC
 122  NEWTON   IAASAC      S   FTY 240-U NB QC LF KD EE

これを効率的に行う方法に関するヒントを探しているだけです。

更新: SSS で提案されているように、streamreader アプローチを使用することになりました。しかし、私は古い Mid 関数を避けて、Substring に固執しようとしました。したがって、私はいくつかのチェックを行い、別の SO 投稿からいくつかのコードを使用する必要がありましたが、どれを覚えていません。とにかくここにあります:

    Dim reader As New StringReader(aSource)
    Dim line As String = Nothing
    Dim writer As New StringWriter
    Dim chunkSize As Integer = 50
    Dim chunk As String

    Do
        line = reader.ReadLine()
        If Not String.IsNullOrEmpty(line) Then
            Debug.WriteLine(line.Length & "-->" & line)
            'if line length is less than or equal to chunk size then write it out, otherwise cut it up and then write the chunks out
            If line.Length <= chunkSize Then
                writer.WriteLine(line)
            Else
                Debug.WriteLine("---------------------")
                For i = 0 To line.Length Step chunkSize
                    Debug.WriteLine("i  =" & i)
                    Debug.WriteLine("i+c=" & i + chunkSize)
                    Debug.WriteLine("L  =" & line.Length)
                    If i + chunkSize > line.Length Then
                        chunk = line.Substring(i, line.Length - i)
                    Else
                        chunk = line.Substring(i, chunkSize)
                    End If
                    Debug.WriteLine("  " & chunk.Length & "-->" & chunk)
                    writer.WriteLine(chunk)
                Next i
                Debug.WriteLine("---------------------")
            End If
        End If
    Loop While (line IsNot Nothing)
    reader.Close()
    reader.Dispose()

    'this cut string now becomes our source
    Debug.WriteLine("==>" & writer.ToString)
    sourceText = writer.ToString

    writer.Close()
    writer.Dispose()

同じ問題を抱えている人に役立つことを願っています。

4

1 に答える 1

0
Imports System.IO
Public Class Form1

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim strFilenameIn As String = "C:\Junk\Input.TXT"
    Dim strFilenameOut As String = "C:\Junk\Output.TXT"
    Try
      Using sr As New StreamReader(strFilenameIn)
        Using sw As New StreamWriter(strFilenameOut)
          Do
            Dim strLine As String = sr.ReadLine()
            If strLine Is Nothing Then Exit Do 'EOF
            For i As Integer = 1 To strLine.Length Step 50
              sw.WriteLine(Mid(strLine, i, 50))
            Next i
          Loop
        End Using
      End Using
    Catch ex As Exception
      MsgBox(ex.Message)
      Exit Sub
    End Try
    MsgBox(strfilenameout & " written")
  End Sub
End Class
于 2012-05-24T04:02:39.217 に答える