find first occurence of multiple characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    find first occurence of multiple characters

    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


  • tshad

    #2
    Re: find first occurence of multiple characters

    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" <tfs@dslextreme .comwrote in message
    news:OkEviueGJH A.3668@TK2MSFTN GP02.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
    >

    Comment

    • James Hahn

      #3
      Re: find first occurence of multiple characters

      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" <tfs@dslextreme .comwrote in message
      news:Og9bJ5eGJH A.468@TK2MSFTNG P06.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" <tfs@dslextreme .comwrote in message
      news:OkEviueGJH A.3668@TK2MSFTN GP02.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
      >>
      >
      >

      Comment

      • Brian Gideon

        #4
        Re: find first occurence of multiple characters

        On Sep 18, 7:06 pm, "tshad" <t...@dslextrem e.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(B yVal 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(star t, i - start)
        End If
        Next
        If start <-1 Then
        Return Integer.Parse(a .Substring(star t))
        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.

        Comment

        • eBob.com

          #5
          Re: find first occurence of multiple characters

          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" <tfs@dslextreme .comwrote in message
          news:Og9bJ5eGJH A.468@TK2MSFTNG P06.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" <tfs@dslextreme .comwrote in message
          news:OkEviueGJH A.3668@TK2MSFTN GP02.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
          >>
          >
          >

          Comment

          • Brian Gideon

            #6
            Re: find first occurence of multiple characters

            On Sep 19, 8:49 am, "eBob.com" <faken...@total lybogus.comwrot e:
            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.

            Comment

            • tshad

              #7
              Re: find first occurence of multiple characters


              "James Hahn" <jhahn@yahoo.co mwrote in message
              news:uoPtpZfGJH A.3756@TK2MSFTN GP06.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.Reg ularExpressions .Match oMatch =
              System.Text.Reg ularExpressions .Regex.Match(Pa th.GetFileName( AppSettings.PCV FileName),
              "(\\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.SectionNumb er);

              So it is obviously assignable.

              And I can do:

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

              System.Text.Reg ularExpressions .Match oMatch =
              System.Text.Reg ularExpressions .Regex.Match(Pa th.GetFileName( AppSettings.PCV FileName),
              "(\\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" <tfs@dslextreme .comwrote in message
              news:Og9bJ5eGJH A.468@TK2MSFTNG P06.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" <tfs@dslextreme .comwrote in message
              >news:OkEviueGJ HA.3668@TK2MSFT NGP02.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
              >>>
              >>
              >>
              >

              Comment

              Working...