Re: Find instance in a string
From: John Fiala (jcfiala523_at_hotmail.com)
Date: 05/26/04
- Next message: John Fiala: "Re: Sharing Data between forms - strong typed"
- Previous message: Mick Doherty: "Re: TabControl"
- In reply to: Chris Thunell: "Find instance in a string"
- Next in thread: Jay B. Harlow [MVP - Outlook]: "Re: Find instance in a string"
- Messages sorted by: [ date ] [ thread ]
Date: 26 May 2004 13:49:52 -0700
"Chris Thunell" <cthunell@pierceassociates.com> wrote in message news:<O1rikEzQEHA.1624@TK2MSFTNGP09.phx.gbl>...
> I'm looking to find in a long string an instance of 4 numbers in a row, and
> pull out those numbers.
> For instance:
> string = "0104 PBR", i'd like to get the 0104.
> string="PBR XT 0105 TD", i'd like to get the 0105.
> The numbers will always be 4 digits together.
> (I'm using vb.net)
> Any help would be greatly appreciated!
> Chris
> cthunell@pierceassociates.com
You want a regular expression, here.
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Call Test("0104 PBR")
Call Test("PBR XT 0105 TD")
Call Test("blah PER VB 0106")
Debug.Assert(True)
End Sub
Private Sub Test(ByVal StringToTest As String)
Dim re As New Regex("\s\d{4}\s|^\d{4}\s|\s\d{4}$|^\d{4}$")
Dim mc As MatchCollection = re.Matches(StringToTest)
Dim m As System.Text.RegularExpressions.Match
For Each m In mc
Console.WriteLine(Trim$(m.ToString()))
Next
End Sub
End Module
(Note that there may be more elegant ways to form the expression, but
I ran into the problem that putting ^ in [] has a different meaning.
Please consult your friendly System.Text.RegularExpressions help
topics for more information.)
(Note also that \s matches not only spaces, but also tabs, formfeeds,
and so forth. If you're sure you only need spaces, then replace the
\s with a space.)
John Fiala
jcfiala523-at-hotmail.com
http://www.livejournal.com/users/fiala_tech/
- Next message: John Fiala: "Re: Sharing Data between forms - strong typed"
- Previous message: Mick Doherty: "Re: TabControl"
- In reply to: Chris Thunell: "Find instance in a string"
- Next in thread: Jay B. Harlow [MVP - Outlook]: "Re: Find instance in a string"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|