473,804 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing between a character and sysmbol

I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
Any help is appreciated it.

Aug 17 '06
22 1515
Doh,

MessageBox.Show (y(0).Substring (y(0).Length - i))

of course,

Cor

"Cor Ligthert [MVP]" <no************ @planet.nlschre ef in bericht
news:uY******** ******@TK2MSFTN GP05.phx.gbl...
>I would have to agree with adm, RegEx is designed specifically for this
very thing. It excels at it.
Grab a copy of Expresso.
But if you are not used to RegEx can a combination from the code from
Scott and Branco help you.

\\\
Dim x As String = "10AF101-25"
Dim y As String() = x.Split("-"c)
Dim i As Integer
For i = y(0).Length - 1 To 0 Step -1
If Not Char.IsDigit((y (0)(i))) Then
Exit For
End If
Next
MessageBox.Show (y(0).Substring (y(0).Length - 3))
///
Tested of course in this case,

And assuming that there is forever at leaset one hyphen in the value.

:-)

Cor

Aug 18 '06 #11
Tom Shelton wrote:
ge*********@gma il.com wrote:
>I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
Any help is appreciated it.\

Another possible solution :)

Option Strict On
Option Explicit On

Imports System
Imports System.Text.Reg ularExpressions

Module RegExDemoModule
Private Const DEFAULT_STRING As String = "10AF101-25"
Private Const REGULAR_EXPRESS ION As String = "[a-zA-Z]+(\d+)-"

Sub Main()
Dim expression As New Regex(REGULAR_E XPRESSION)
Dim theMatch As Match = expression.Matc h(DEFAULT_STRIN G)

Console.WriteLi ne(theMatch.Gro ups(1).ToString ())
End Sub

End Module
I like this one the best... except for that constants naming convention!
I'm getting COBOL flashbacks! :)

--
Larry Lard
la*******@googl email.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Aug 18 '06 #12
Thanks for everyone help on this. I have tried regex by using this
expression [\d]+-
It almost provides the result that I want except that it brings back
the "-" character also so I get 101- instead of 101.

Any ideas?

Thanks

Aug 18 '06 #13

ge*********@gma il.com wrote:
Thanks for everyone help on this. I have tried regex by using this
expression [\d]+-
It almost provides the result that I want except that it brings back
the "-" character also so I get 101- instead of 101.

Any ideas?

Thanks
(\d+)-

Then, you can get the 101 by referencing element 1 of the groups
collection...

--
Tom Shelton [MVP]

Aug 18 '06 #14

Larry Lard wrote:
Tom Shelton wrote:
ge*********@gma il.com wrote:
I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-" and
stops when it encounters any string character like AF

So my result should be 101.
Any help is appreciated it.\
Another possible solution :)

Option Strict On
Option Explicit On

Imports System
Imports System.Text.Reg ularExpressions

Module RegExDemoModule
Private Const DEFAULT_STRING As String = "10AF101-25"
Private Const REGULAR_EXPRESS ION As String = "[a-zA-Z]+(\d+)-"

Sub Main()
Dim expression As New Regex(REGULAR_E XPRESSION)
Dim theMatch As Match = expression.Matc h(DEFAULT_STRIN G)

Console.WriteLi ne(theMatch.Gro ups(1).ToString ())
End Sub

End Module

I like this one the best... except for that constants naming convention!
I'm getting COBOL flashbacks! :)
Not COBOL fro me, but C/C++. I still have hard time letting that one
go :)

--
Tom Shelton [MVP]

Aug 18 '06 #15
There are several things wrong with your code. First, Using IsNumeric can
lead to anomolous results.
Not when testing single characters it won't. It will only produce
unanticipated results when testing strings of more than 1 charactor in
length.
Second, your loop bounds are not correct and will cause an exception.
True, I forgot to subtract 1 from the upper boundry.
Third, Your loop scans forward, so the result from the example string will
be "10", not the desired "101".
Don't know what you are saying here, the loop will produce: "10101" as
desired.
I would have to agree with adm, RegEx is designed specifically for this
very thing. It excels at it.
I don't disagree, but RegEx is difficult for most who are not accustom to
it.

Here is the corrected code (tested) that works like a charm:

Dim x As String = "10AF101-25"
Dim y As String() = x.Split("-")
Dim z As String = ""

Dim i As Integer
For i = 0 To y(0).Length - 1
If IsNumeric(y(0)( i)) Then z &= y(0)(i)
Next
Grab a copy of Expresso.

-Boo
>Dim x As String = "10AF101-25"
Dim y As String() = x.split("-")
Dim z As String
dim i As Integer
For i = 0 To y(0).length
If IsNumeric(y(i)) Then z = y &= y(i)
Next
<ge*********@g mail.comwrote in message
news:11******* *************** @75g2000cwc.goo glegroups.com.. .
>>I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-"
and stops when it encounters any string character like AF

So my result should be 101.

Any help is appreciated it.


Aug 18 '06 #16
Scott M. wrote:
<snip>
Don't know what you are saying here, the loop will produce: "10101" as
desired.
<snip>
Here is the corrected code (tested) that works like a charm:

Dim x As String = "10AF101-25"
Dim y As String() = x.Split("-")
Dim z As String = ""

Dim i As Integer
For i = 0 To y(0).Length - 1
If IsNumeric(y(0)( i)) Then z &= y(0)(i)
Next
<snip>

Notice that the OP asked for the digits that come before the '-' up to
the first non-digit. As the OP points out, in the example string, the
result should be the digits between "10AF" and "-25", i.e., 101.

Your code returns all the numeric chars up to the '-', just
disregarding the intervening letters, that is, 10101, which is wrong.

Even if that was the case, notice that it's not recommended to build a
string like this, but to use a StringBuilder, instead. Also, accessing
array items inside a loop should be avoided whenever possible. If the
OP had asked for all numeric chars before the '-', then a better method
would be:

Function ExtractDigits(T ext As String) As String
Dim S As New System.Text.Str ingBuilder
For Each C As Char in Text
If C = "-"c Then Exit For
If Char.IsDigit(C) Then S.Append(C)
Next
Return S.ToString
End Function

Regards,

Branco.

Aug 19 '06 #17
Scott,

I see now that a single E character can never be an exponent, I did not test
the behaviour of a single plus in this. However to avoid that do I find the
isDigit more describtive and does not even need thinking about the problems
that can be with IsNumeric.

Just my idea,

Cor

"Scott M." <s-***@nospam.nosp amschreef in bericht
news:uf******** ********@TK2MSF TNGP03.phx.gbl. ..
>There are several things wrong with your code. First, Using IsNumeric
can lead to anomolous results.

Not when testing single characters it won't. It will only produce
unanticipated results when testing strings of more than 1 charactor in
length.
>Second, your loop bounds are not correct and will cause an exception.

True, I forgot to subtract 1 from the upper boundry.
>Third, Your loop scans forward, so the result from the example string
will be "10", not the desired "101".

Don't know what you are saying here, the loop will produce: "10101" as
desired.
>I would have to agree with adm, RegEx is designed specifically for this
very thing. It excels at it.

I don't disagree, but RegEx is difficult for most who are not accustom to
it.

Here is the corrected code (tested) that works like a charm:

Dim x As String = "10AF101-25"
Dim y As String() = x.Split("-")
Dim z As String = ""

Dim i As Integer
For i = 0 To y(0).Length - 1
If IsNumeric(y(0)( i)) Then z &= y(0)(i)
Next
>Grab a copy of Expresso.

-Boo
>>Dim x As String = "10AF101-25"
Dim y As String() = x.split("-")
Dim z As String
dim i As Integer
For i = 0 To y(0).length
If IsNumeric(y(i)) Then z = y &= y(i)
Next
<ge*********@ gmail.comwrote in message
news:11****** *************** *@75g2000cwc.go oglegroups.com. ..

I have a string like the following:

10AF101-25

I would like to extract any numerical number that precedes the "-"
and stops when it encounters any string character like AF

So my result should be 101.

Any help is appreciated it.



Aug 19 '06 #18
Notice that the OP asked for the digits that come before the '-' up to
the first non-digit. As the OP points out, in the example string, the
result should be the digits between "10AF" and "-25", i.e., 101.
No, I didn't notice that, but my code could easily be changed to accomodate
that.
Even if that was the case, notice that it's not recommended to build a
string like this, but to use a StringBuilder, instead.
Uh, no that is not true. StringBuilders are very good and I agree that for
large strings and large amounts of manipulations, StringBuilders are
effiient. But no one says strings shouldn't be built manually in all cases.
For small strings and limited amounts of manipulations, manually creating a
string is perfectly fine. In fact, in many cases, because of the intern
pool, there may be no gain in using StringBuilders. And actually, if the
string in question is small, using a StringBuilder may actually use MORE
memory than not using one.
>Also, accessing array items inside a loop should be avoided whenever
possible.
That's ridiculous! Where did you get that from? One of the benefits of
arrays and collections is the ease of iterating through them via loops.
Your code is perfectly fine, but please don't make up best practices just to
bolster your code over others.

If the
OP had asked for all numeric chars before the '-', then a better method
would be:

Function ExtractDigits(T ext As String) As String
Dim S As New System.Text.Str ingBuilder
For Each C As Char in Text
If C = "-"c Then Exit For
If Char.IsDigit(C) Then S.Append(C)
Next
Return S.ToString
End Function

Regards,

Branco.

Aug 19 '06 #19
I don't disagree Cor, but you'll NEVER have a problem checking any single
character with IsNumeric. Only digits will return true. Where you can get
into trouble with IsNumeric is with values that start with a number and then
contain a non-numeric value further down the string like "1024X". But
checking one char at a time is not a problem.
"Cor Ligthert [MVP]" <no************ @planet.nlwrote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
Scott,

I see now that a single E character can never be an exponent, I did not
test the behaviour of a single plus in this. However to avoid that do I
find the isDigit more describtive and does not even need thinking about
the problems that can be with IsNumeric.

Just my idea,

Cor

"Scott M." <s-***@nospam.nosp amschreef in bericht
news:uf******** ********@TK2MSF TNGP03.phx.gbl. ..
>>There are several things wrong with your code. First, Using IsNumeric
can lead to anomolous results.

Not when testing single characters it won't. It will only produce
unanticipate d results when testing strings of more than 1 charactor in
length.
>>Second, your loop bounds are not correct and will cause an exception.

True, I forgot to subtract 1 from the upper boundry.
>>Third, Your loop scans forward, so the result from the example string
will be "10", not the desired "101".

Don't know what you are saying here, the loop will produce: "10101" as
desired.
>>I would have to agree with adm, RegEx is designed specifically for this
very thing. It excels at it.

I don't disagree, but RegEx is difficult for most who are not accustom to
it.

Here is the corrected code (tested) that works like a charm:

Dim x As String = "10AF101-25"
Dim y As String() = x.Split("-")
Dim z As String = ""

Dim i As Integer
For i = 0 To y(0).Length - 1
If IsNumeric(y(0)( i)) Then z &= y(0)(i)
Next
>>Grab a copy of Expresso.

-Boo

Dim x As String = "10AF101-25"
Dim y As String() = x.split("-")
Dim z As String
dim i As Integer
For i = 0 To y(0).length
If IsNumeric(y(i)) Then z = y &= y(i)
Next
<ge********* @gmail.comwrote in message
news:11***** *************** **@75g2000cwc.g ooglegroups.com ...

I have a string like the following:
>
10AF101-25
>
I would like to extract any numerical number that precedes the "-"
and stops when it encounters any string character like AF
>
So my result should be 101.
>
Any help is appreciated it.
>




Aug 19 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3002
by: Kylotan | last post by:
I have a text file where the fields are delimited in various different ways. For example, strings are terminated with a tilde, numbers are terminated with whitespace, and some identifiers are terminated with a newline. This means I can't effectively use split() except on a small scale. For most of the file I can just call one of several functions I wrote that read in just as much data as is required from the input string, and return the...
3
2254
by: Fuzzyman | last post by:
I want to parse some text and generate an output that is similar but not identical to the input. The string I produce will be of similar length to the input string - but a bit longer. I'm parsing character by character and adding the characters of the input string to the output until I come to ones I want to modify. This means creating a new string for every character (since strings are immutable) which seems very inneficient -...
19
4013
by: ARK | last post by:
I am writing a search program in ASP(VBScript). The user can enter keywords and press submit. The user can separate the keywords by spaces and/or commas and key words may contain plain words, single quoted strings (phrases), double quoted strings (phrases). For example: Keywords: Jack, Jill, Jim, "Timothy Brown", Mary OR
4
5272
by: Earl | last post by:
I'm curious if there are others who have a better method of accepting/parsing phone numbers. I've used a couple of different techniques that are functional but I can't really say that I'm totally happy with either. 1. My first technique was to restrict the users to entries that could only be 3 character, 3 characters, 4 character (area code, prefix, suffix, respectively). I would null out any inputs that were non-numeric (except the...
1
2694
by: David | last post by:
I have rows of 8 numerical values in a text file that I have to parse. Each value must occupy 10 spaces and can be either a decimal or an integer. // these are valid - each fit in a 10 character block 123.8 123.8 1234.567 12345 12345 1234.567
6
2078
by: Tuomas Rannikko | last post by:
Hello, I'm currently writing a XML processor for the fun of it. There is something I don't understand in the spec though. I'm obviously missing something important. The spec states that both Internal General and Character references are included when referenced in content. And "included" means: <quote>
2
4889
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important data reading. For instance this is my buffer string: 012301234FFFFFFxFFFFFFxFFFFFFx Where the FFFFFF is my Hex data I need to read. I am using the "x" as a separater as I was having problems using the VbCrLf. But I think
13
4529
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
1
3504
by: Sidhartha | last post by:
Hi, I am facing a problem while parsing local language characters using sax parser. We use DOM to parse and SAX to read the source. But when our application parses strings with local language especially czech,polish,turkish in place of local language character some other word is comming. Eg: Input string :ahoj, jak se máš Output string :ahoj, jak se m&aacute;š
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9176
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7643
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.