Help extracting text from a text file in VB
Using VB 2008, I need help extracting text from a loaded text file. I turn the text into a string and know I will need to use split() in there but I'm unsure how to go about this properly and was hoping someone could point me to some code snippets or let me know which functions I should be looking at and how to approach this.
The text will be of varying length and I need to strip out certain information. In this example, the information needed will be between two tags.. ie <x>sometext</x>
I need to get all of the text in between those tags. Additionally, the file will be of varying lengths, so the tags can repeat an unlimited # of times.
In this file the text is xml however I will need to do the same on other files which will not be xml, therefore using some of the xml parsing code I've found wouldn't work.
Can anyone help out?
If the file is XML, you should use XML/XPath:
For unstructured text, use regular expressions:
' Imports System.Xml
' Imports System.Xml.XPath
Dim Document As New XmlDocument()
' Find all the text between <x>...</x> tags
Dim Nodes As XmlNodeList = Document.SelectNodes("//x")
For Each Node As XmlNode In Nodes
' Imports System.IO
' Imports System.Text.RegularExpressions
Dim StartText As String = "<x>"
Dim EndText As String = "</x>"
Dim Text As String
Using sr As StreamReader = File.OpenText(FileName)
Text = sr.ReadToEnd
Dim Pattern As String = StartText & "(.+)" & EndText
Dim Matches As MatchCollection = Regex.Matches(Text, Pattern)
For Each m As Match In Matches
Last edited by Phil Weber; 05-10-2008 at 06:06 PM.
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
Thank you for your help... took some time for me to get it down right but it works like a charm.
By nobbyap in forum VB Classic
Last Post: 11-26-2008, 04:44 AM
By Kevin in forum VB Classic
Last Post: 12-05-2005, 07:25 PM
By David in forum VB Classic
Last Post: 09-04-2001, 08:38 AM
By George Gilbert in forum vb.announcements
Last Post: 08-19-2001, 12:34 PM
By deborah in forum authorevents.kurata
Last Post: 04-17-2000, 02:33 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center