Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expressions

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US

I want to basically mimic a regular expression function in VB... any advice as to how I'd go about this?

1/21/03 10:22 AM 242327 <A HREF=&quot;/lots/of/path/info/myFile.txt&quot;>myFile.txt</A>

I have the above line, and I'd like to extract myFile.txt

So easy with some languages, but I don't see any regular expression support in VB beyond like... thanks for any help.

-Rob
 
first hit on google, but it seems they want $200 US for it:

But since this is a .net forum, how about the second hit
Code:
System.Text.RegularExpressions
:

and a little example function using it:

Never used any of it (or even anything .net for that matter) but hope it helps. I'm being thrown into some .net framework so I decided to look around some. If you need any help with the regex itself, let me know. I've become a perl monkey lately and we love those things :) ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
The following function will do the trick:

Public Function GetFileName(ByVal sIn As String) As String
Const cPat As String = &quot;(?<=>)(.*?)(?=</A>)&quot;
Try
dim rx As New System.Text.RegularExpressions.Regex(cPat)
Return (rx.Match(sIn).ToString)
Catch ex As Exception
Stop
End Try
End Function

'=========
The first part of the pattern establishes the start point for the match. The term ?<= defines that a match must be preceded by the supplied value -in this case.

The .* in the second part is what return you the file name. It matches any number of any character, the ? suffix tells it not to be greedy, that it should let the next expression try for a match first.

The third expression looks forward for the string </A>, but doesn't include it in the match.

If you want to cater for lower case, replace the /A with /[Aa]

 
Also those nice people at wrox now do a vb.net text manipulation handbook that goes into great detail about .net's regex capabilities. You might want to look it up.
One of these days I'm going to calculate how much money I've spent on their books and be truly horrified(they are good though).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top