473,465 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Find instance in a string

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
7 3959
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
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
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
>
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lothar Behrens | last post by:
Hi, this is an unusual question here, but I think, it is C++ related due to the fact that my code is written with it. I have written an ODBC wrapper that - if checked out with my last working...
10
by: Sean Berry | last post by:
I need to find the second to last occurence of a "." in a string. Basically I am taking a URL like http://this.is.mydomin.com/path/to/file.txt and want to extract /path/to/file.txt I...
5
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
14
by: inpuarg | last post by:
I want to find a & character using Regex. But not && How can i manage this in c# Quickfind window ? -------------------------------------------------- ne kadar yaşarsan yaşa sevdiğin kadardır...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
2
by: moondaddy | last post by:
I had to repost this because I had to update and change my msdn alias. I will re-ask the question and clarify a few things that were not clear before. This code is all executed on my dev...
0
by: db2user24 | last post by:
I'm trying to invoke a DB2 stored procedure. The stored proc is coded in C and compiled to a shared library, which has been placed in the <DB2 dir>/functions directory. The platform is Linux (using...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
3
by: srinivasan srinivas | last post by:
Hi, I have written a class which has some attributes. I want to know how do i find out the size of an instance of this class?? class T(object):     def __init__(self, fn_name, *args, **kwds):    ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.