find first occurence of multiple characters

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

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
 
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
 
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.
 
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.
 
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
 
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.
 
James Hahn said:
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.GetFileName(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.GetFileName(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
 
Back
Top