Hi

I'm trying to extract certain parts of each line of a text file.
An example of the lines is as such:

[text] [number]
[text] [number]
[text] [number]

...And so on.

I would like the text and number from each line as a separate element in an array of strings.

So for example, the array could be {"text number", "text number", "text number"}

I could easily do this using the .Split() function or something similar, but the problem is i do not want the '[' and '[' included in the strings.

Further complicating the matter, the text can also contain ''. For example:

[text]more[text] [1234567]
[textmore[text] [7654321]

...etc.

Sorry if I'm being unclear as to what I am trying to achieve...Any help would be greatly appreciated.

Thanks in advance!

So you need a tiny parser:

Dim StringIn As String
Dim StrArr() As String
Dim TempText As String
Dim TempNumber As String
Dim TempPos As Integer
Dim i As Integer

StringIn = TextBox1.Text

TempText = ""
TempNumber = ""
' Split to lines
StrArr = StringIn.Split(CChar(Environment.NewLine))
For i = 0 To StrArr.GetUpperBound(0)
  ' Search the beginning of the number
  TempPos = StrArr(i).LastIndexOf("[")
  If TempPos > 0 Then
    TempNumber = StrArr(i).Substring(TempPos, StrArr(i).Length - TempPos)
    ' Remove "[" and "]" characters from the number
    TempNumber = TempNumber.Replace("[", "").Replace("]", "")
    ' Remove number part from the original string
    StrArr(i) = StrArr(i).Substring(0, TempPos)
  End If
  ' Search the beginning of the "text" part
  TempPos = StrArr(i).IndexOf("[")
  If TempPos >= 0 Then
    ' Remove first "["
    TempText = StrArr(i).Substring(TempPos + 1, StrArr(i).Length - TempPos - 1)
  End If
  TempPos = TempText.LastIndexOf("]")
  If TempPos >= 0 Then
    ' Remove last "]"
    TempText = TempText.Substring(0, TempPos)
  End If
  ' Rebuild the string
  StrArr(i) = TempText & " " & TempNumber
Next i

It works with the samples you gave. If there are more "special" cases you may have to modify the code above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.