Parsing between a character and sysmbol

  • Thread starter Thread starter gene.ariani
  • Start date Start date
G

gene.ariani

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.
 
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
 
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.
<snip>

The function bellow may help:

Function ScanInt(ByVal T As String) As String
'Finds the position of the "-",
'counting from the last char
Dim P As Integer = T.LastIndexOf("-")

'Scans the text backward until
'a non noumeric item is found
Dim I As Integer
For I = P - 1 To 0 Step -1
If Not Char.IsDigit(T(I)) Then
Exit For
End If
Next

'I is referencing the first non-digit char
I += 1
If I < P Then
Return T.Substring(I, P - I)
Else
Return String.Empty
End If
End Function

Regards,

B.
 
This seems much more complicated than it need be. Why search for the "-"
when you can immediately split the string at its location and throw away the
part you don't need?
 
Hello Scott M.,

There are several things wrong with your code.
First, Using IsNumeric can lead to anomolous results.
Second, your loop bounds are not correct and will cause an exception.
Third, Your loop scans forward, so the result from the example string will
be "10", not the desired "101".

I would have to agree with adm, RegEx is designed specifically for this very
thing. It excels at it.
Grab a copy of Expresso.

-Boo
 
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.RegularExpressions

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

Sub Main()
Dim expression As New Regex(REGULAR_EXPRESSION)
Dim theMatch As Match = expression.Match(DEFAULT_STRING)

Console.WriteLine(theMatch.Groups(1).ToString())
End Sub

End Module
 
Scott said:
This seems much more complicated than it need be. Why search for the "-"
when you can immediately split the string at its location and throw away the
part you don't need?

Because split does just that: searches for a pattern. And then allocate
an array. And then populate each member of the array with a copy of the
slice between the separator.

It just bothers me to use Split to do that. Just as IsNumeric: it works
by converting the string to Double, what seems a terrible waste of
processing, to me.

The code seems too much effort but it also seems efficient, to me.
YMMV.

Regards,

Branco.
 
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
 
Tom said:
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.RegularExpressions

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

Sub Main()
Dim expression As New Regex(REGULAR_EXPRESSION)
Dim theMatch As Match = expression.Match(DEFAULT_STRING)

Console.WriteLine(theMatch.Groups(1).ToString())
End Sub

End Module

I like this one the best... except for that constants naming convention!
I'm getting COBOL flashbacks! :)
 
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
 
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...
 
Larry said:
Tom said:
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.RegularExpressions

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

Sub Main()
Dim expression As New Regex(REGULAR_EXPRESSION)
Dim theMatch As Match = expression.Match(DEFAULT_STRING)

Console.WriteLine(theMatch.Groups(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 :)
 
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
 
Scott M. wrote:
Don't know what you are saying here, the loop will produce: "10101" as
desired.
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(Text As String) As String
Dim S As New System.Text.StringBuilder
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.
 
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
 
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
 
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.
 
Back
Top