Re: Find instance in a string

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

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 05/27/04


Date: Wed, 26 May 2004 19:00:49 -0500

Chris,
I would use a RegEx as the others stated, something like:

    Private Shared Function FindNumber(ByVal input As String) As String
        Static exp As New Regex("\d{4}", RegexOptions.Compiled)
        Dim match As Match = exp.Match(input)
        Return match.Groups(0).Value
    End Function

    Public Shared Sub Main()
        Debug.WriteLine(FindNumber("0104 PBR"), "0104 PBR")
        Debug.WriteLine(FindNumber("PBR XT 0105 TD"), "PBR XT 0105 TD")
        Debug.WriteLine(FindNumber("PBR XT 105 TD"), "PBR XT 105 TD")
    End Sub

If there is no 4 character number in the input, an empty string is returned
otherwise the string itself is returned. If there are more then a single
instance of 4 numbers only the first is returned.

If you need multiple matches you can use Match.Success & Match.NextMatch in
a loop, or I believe John's code.

        Dim match As Match = exp.Match(input)
        Do While match .Success
            Debug.WriteLine(match.Groups(0).Value, input)
            match = match.NextMatch()
        Loop

The following sites provide a wealth of information on regular expressions.

A tutorial & reference on using regular expressions:
http://www.regular-expressions.info/

The MSDN's documentation on regular expressions:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconRegularExpressionsLanguageElements.asp

Hope this helps
Jay

"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
>
>



Relevant Pages

  • Re: bad table design
    ... because this is handled differently in different databases. ... are RDBMSses which consider an empty string equal to ... Using a single character as a default might conflict ...
    (microsoft.public.sqlserver.server)
  • Re: a syntax error I cannot see.
    ... regular expressions are "first class ... record up to the matched string. ... whole line where is the matched string up to the matched string. ... then zero or more repetiions of a backslash character". ...
    (comp.lang.awk)
  • Re: regexp: not matching a sequence of characters
    ... > specific characters, ... The problem you're trying to tackle is hard with regular expressions -- ... The "greedy match" problem (matching the longest possible string) is ... it's about Perl regular expressions. ...
    (comp.lang.php)
  • Regular Expressions: "Negated Strings" instead of "Negated Character Classes"
    ... With Perl regular expressions one can ... define a character and negated character classes. ... removed from a string ... paragraph- and divider-tags from a string ...
    (comp.lang.perl.misc)
  • Re: Occurances in bash shell script regular expressions
    ... This frequently trips up people who are new to regular expressions. ... that 0 occurrences of the character class is OK, ... Heck, maybe *every* string. ...
    (comp.os.linux.misc)