Chris,
Doh! Here's an improved version of mine. (my earlier version would return
the first 4 numbers of a 5 or greater instance of a number.)
Private Shared Function FindNumber(ByVal input As String) As String
Static exp As New Regex("(^|\D+)(?'number'\d{4})($|\D+)",
RegexOptions.Compiled)
Dim m As Match = exp.Match(input)
Return m.Groups("number").Value
End Function
Public Shared Sub Main()
Const test1 As String = "0104 PBR"
Const test2 As String = "PBR XT 0105 TD"
Const test3 As String = "PBR XT 105 TD"
Const test4 As String = "PBR 0104 XT 0105 TD"
Const test5 As String = "PBR XT 01050 TD"
Debug.WriteLine(FindNumber(test1), test1)
Debug.WriteLine(FindNumber(test2), test2)
Debug.WriteLine(FindNumber(test3), test3)
Debug.WriteLine(FindNumber(test4), test4)
Debug.WriteLine(FindNumber(test5), test5)
End Sub
The biggest difference between this one & John's is that I look for any non
numeric \D before or after the number, where as John looks for any
whitespace \s character.
See the links I gave earlier for further features of regular expressions.
Hope this helps
Jay
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:O1**************@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
ct******@pierceassociates.com