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

find first occurence of multiple characters

P: n/a
I have a filename that I want to extract the 1st set of numbers up to either
a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom
Sep 19 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers until
I get a non-numeric then stop. I can also do this in a for-loop but I was
looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom

Sep 19 '08 #2

P: n/a
Take a look at the MatchCollection class. This will return the matches from
the regular expression as a collection, each element of which has a value
(or ToString), length and index, whch is the position in the source where
the matched substring occured. If you construct your regular expression
properly then item 0 (which might be null) is the portion you are after.

"tshad" <tf*@dslextreme.comwrote in message
news:Og*************@TK2MSFTNGP06.phx.gbl...
Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers
until I get a non-numeric then stop. I can also do this in a for-loop but
I was looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom

Sep 19 '08 #3

P: n/a
On Sep 18, 7:06*pm, "tshad" <t...@dslextreme.comwrote:
I have a filename that I want to extract the 1st set of numbers up to either
a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom
Well, if you write a function that does this then from the caller's
perspective it would be one statement.

Public Function ExtractNumber(ByVal text As String) As Integer
Dim start As Integer = -1
For i As Integer = 0 To text.Length
If start = -1 Then
If IsNumeric(a(i)) Then
start = i
End If
ElseIf Not IsNumeric(a(i)) Then
Return Integer.Parse(a.Substring(start, i - start)
End If
Next
If start <-1 Then
Return Integer.Parse(a.Substring(start))
End If
Return 0
End Function

You could use a regular expression as an alternative. The code would
probably be shorter, but not necessarily more readable.
Sep 19 '08 #4

P: n/a
This should be pretty easy using regular expressions. As James Hahn
suggested look at MatchCollection. Download Expresso from UltraPico so that
you can experiment with and test various solutions. I think that the
essence of your regex will be /d+ but the devil will be in the details. If
any filename can be thrown at you there might be cases such as x449231~1.xml
which may or may not meet your criterion. If necessary, there is someway to
say that a match must occur at the beginning of a string, I forget the
syntax offhand.

Good Luck, Bob

"tshad" <tf*@dslextreme.comwrote in message
news:Og*************@TK2MSFTNGP06.phx.gbl...
Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers
until I get a non-numeric then stop. I can also do this in a for-loop but
I was looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st non
number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom


Sep 19 '08 #5

P: n/a
On Sep 19, 8:49*am, "eBob.com" <faken...@totallybogus.comwrote:
This should be pretty easy using regular expressions. *As James Hahn
suggested look at MatchCollection. *Download Expresso from UltraPico sothat
you can experiment with and test various solutions. *I think that the
essence of your regex will be /d+ but the devil will be in the details. *If
any filename can be thrown at you there might be cases such as x449231~1.xml
which may or may not meet your criterion. *If necessary, there is someway to
say that a match must occur at the beginning of a string, I forget the
syntax offhand.
Use the ^ character to anchor the match at the beginning on the input.
Sep 19 '08 #6

P: n/a

"James Hahn" <jh***@yahoo.comwrote in message
news:uo**************@TK2MSFTNGP06.phx.gbl...
Take a look at the MatchCollection class. This will return the matches
from the regular expression as a collection, each element of which has a
value (or ToString), length and index, whch is the position in the source
where the matched substring occured. If you construct your regular
expression properly then item 0 (which might be null) is the portion you
are after.
That was exactly what I did.

But then I ran into another problem.

I am moving this into an SqlParameter, but I can't seem move it directly
into the parameter.Value.

SqlParameter parameter = new SqlParameter("@" +
"PCVOrderID", SqlDbType.Int);

System.Text.RegularExpressions.Match oMatch =
System.Text.RegularExpressions.Regex.Match(Path.Ge tFileName(AppSettings.PCVFileName),
"(\\d*)");

Int32.TryParse(oMatch.Groups[1].Value, out
(int)(parameter.Value));

The error I get is:

a ref or out arguement must be an assignable value.

This is.

I tried using

parameter.Value
(int)parameter.Value

And got the same error.

I can do:

parameter.Value = Convert.ToInt32(tf.SectionNumber);

So it is obviously assignable.

And I can do:

parameter = new SqlParameter("@" + "PCVOrderID",
SqlDbType.Int);

System.Text.RegularExpressions.Match oMatch =
System.Text.RegularExpressions.Regex.Match(Path.Ge tFileName(AppSettings.PCVFileName),
"(\\d*)");

Int32.TryParse(oMatch.Groups[1].Value, out iTemp);
parameter.Value = iTemp;
cmd.Parameters.Add(parameter);

Why can't I put parameter.Value there?

Thanks,

Tom
"tshad" <tf*@dslextreme.comwrote in message
news:Og*************@TK2MSFTNGP06.phx.gbl...
>Maybe a substring with a regular expression? I know how to strip
non-numerics using a regular expression but how do I grab the numbers
until I get a non-numeric then stop. I can also do this in a for-loop
but I was looking to see if there was a different way.

I don't have to do this, but was curious if there was a way.

Thanks,

Tom

"tshad" <tf*@dslextreme.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...
>>>I have a filename that I want to extract the 1st set of numbers up to
either a ".", "-", "_" or "~" and make that an int.

Or I guess easier just take all the values that are 0-9 up to the 1st
non number value.

So that if I have a value 449231.xml or 449231~1.xml or 449231-1a.xml, I
want to get the 449231 only and make that an integer.

Can that be done in one statement?

Thanks,

Tom


Sep 19 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.