Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Extract all quoted strings with the RegExp object

Total Hit ( 3701)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


When parsing a text file that contains quoted strings - such as a VB source file - you might want to quickly locate and extract all the quoted strings. Thanks to regular expressions and the RegExp object in the Microsoft VBScript Regular Expression type library, this task is surprisingly easy:

Click here to copy the following block
Dim re As New RegExp
Dim ma As Match

' the pattern ".*" means a double quote char followed by any number
' of chars and finally another double quote char
re.Pattern = """.*"""  
' we want all occurrences
re.Global = True
' assumes that the text to be parsed is in the txtSource textbox
For Each ma In re.Execute(txtSource.Text)
  Print ma.Value & " at index " & ma.FirstIndex
Next

The following routine has only one drawback: it doesn't account for repeated double quote chars, which should translate to a single double quote. See how you can remedy to this problem:

Click here to copy the following block
Dim re As New RegExp
Dim ma As Match
Dim tmpText As String
Dim maText As String

' the pattern ".*" means a double quote char followed by any number
' of chars and finally another double quote char
re.Pattern = """.*"""  
' we want all occurrences
re.Global = True
' get the text to be parsed from the txtSource textbox, but
' replace repeated double quotes with an unprintable chars
tmpText = Replace(txtSource.Text, """""", vbNullChar & vbNullChar)

' search all the occurrences
For Each ma In re.Execute(tmpText)
  ' display this occurrence, but restore embedded double quotes
  tmpText = Replace(ma.Value, vbNullChar & vbNullChar, """""")
  Print tmpText & " at index " & ma.FirstIndex
Next



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.