Re: Find instance in a string

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: John Fiala (jcfiala523_at_hotmail.com)
Date: 05/26/04


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/



Relevant Pages

  • Re: Need help understanding a TCL expression
    ... > I highly suspect that this regular expression either does not do ... Michael did an excellent job at explaining most of the command. ... string needs to be substituted before being passed to regexp ... meaning to regexp, so it is save to use it like this. ...
    (comp.lang.tcl)
  • Re: Get regular expression
    ... own tree structure. ... Expression compares a string character-by character, ... regular expression solution, which was about as close as one could get to ... the structure of the hierarchy can be inferred by using ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Get regular expression
    ... regular expression solution, which was about as close as one could get to ... first string. ... explode "ABLATION" and see subnodes of "ENDOMETRIAL ... "Heart 27.33/2" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regular expression optimization
    ... position in the replacement array of strings, ... > input string and a MatchEvaluator delegate. ... > The first part required combining the separate Regular Expression strings ...
    (microsoft.public.dotnet.general)
  • Re: How do I evaluate a JSON response?
    ... There cannot be such a regular expression in ECMAScript as it does ... "7.8.4 String Literals". ... had two different kinds of character escape sequences in one character ... Prototype.js was written by people who don't know javascript for people ...
    (comp.lang.javascript)