By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Find instance in a string

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <O1**************@TK2MSFTNGP09.phx.gbl>,
ct******@pierceassociates.com says...
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)


Look at String.IndexOf or VB's Instr() function.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 20 '05 #2

P: n/a
Hi Chis,

I seldom say this, however this looks as a real Regex problem.
Something I do not like, so I changed the subject a little bit to get
attention from the ones who like Regex problems and that are a lot in this
newsgroup.

Cor
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

Nov 20 '05 #3

P: n/a
Hi Chris,

If you know the string you are looking for you can do String.IndexOf()

On the other hand of you are looking for the first occurance of 4
consecutive numbers in a string you could do some regular expressions.

I have only tried this once in VB Script and I think its changed a bit
since. Your regular expression string can be something like
"[0-9]{4}"

You could also take the easier route and parse the string character by
charcater. But regular expressions will be more efficient.

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

Nov 20 '05 #4

P: n/a
>
I have only tried this once in VB Script and I think its changed a bit
since. Your regular expression string can be something like
"[0-9]{4}"

Just tried it and it works as expected.

Public Sub Foo(ByVal sInputString As String)
Dim r As New Regex("[0-9]{4}")
Dim m As Match = r.Match(sInputString)
If m.Success Then
Debug.WriteLine(m.Value)
Else
Debug.WriteLine("No matches")
End If
End Sub

I hope that helps.

--
Wazir



Nov 20 '05 #5

P: n/a
"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


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/
Nov 20 '05 #6

P: n/a
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/de...geElements.asp

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

Nov 20 '05 #7

P: n/a
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

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.