Extracting numbers from a string

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

    Extracting numbers from a string

    I have a number of strings that represents time.

    1w 2d 3h 15m
    2d 3h 15m
    4h 30m
    45m

    I want to extract the number parts of my strings into separate
    variables for Weeks, Days, Hours and Minutes.

    Any body got any good ideas about how to do this?

  • Armin Zingler

    #2
    Re: Extracting numbers from a string

    "Dan" <dan_williams@n ewcross-nursing.comschr ieb
    I have a number of strings that represents time.
    >
    1w 2d 3h 15m
    2d 3h 15m
    4h 30m
    45m
    >
    I want to extract the number parts of my strings into separate
    variables for Weeks, Days, Hours and Minutes.
    >
    Any body got any good ideas about how to do this?




    Armin

    Comment

    • Harry

      #3
      Re: Extracting numbers from a string

      "Dan" <dan_williams@n ewcross-nursing.comwrot e in message
      news:1191515519 .352696.238000@ o80g2000hse.goo glegroups.com.. .
      >I have a number of strings that represents time.
      >
      1w 2d 3h 15m
      2d 3h 15m
      4h 30m
      45m
      >
      I want to extract the number parts of my strings into separate
      variables for Weeks, Days, Hours and Minutes.
      >
      Any body got any good ideas about how to do this?
      >
      Air code:
      The string.Split method will let you build an array of each line eg myArray
      = sLine.split(" ")
      You could use the string.Contains method to identify if it is a week hour
      etc
      Perhaps an array of Stuctures to store the rows of data in...
      Just a couple of ideas.....


      Comment

      • eBob.com

        #4
        Re: Extracting numbers from a string

        I'd look into regular expressions.

        Good Luck, Bob

        "Dan" <dan_williams@n ewcross-nursing.comwrot e in message
        news:1191515519 .352696.238000@ o80g2000hse.goo glegroups.com.. .
        >I have a number of strings that represents time.
        >
        1w 2d 3h 15m
        2d 3h 15m
        4h 30m
        45m
        >
        I want to extract the number parts of my strings into separate
        variables for Weeks, Days, Hours and Minutes.
        >
        Any body got any good ideas about how to do this?
        >

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Extracting numbers from a string

          Dan,

          Like Harry I would just simple use the split for this.

          Cor

          Comment

          • Dan

            #6
            Re: Extracting numbers from a string


            I was hoping i could do it in one line with a regular expression
            rather than creating a function that splits it and searches for
            specific things.

            Anybody got any example regular expressions i could use?

            Comment

            • rowe_newsgroups

              #7
              Re: Extracting numbers from a string

              On Oct 5, 6:33 am, Dan <dan_willi...@n ewcross-nursing.comwrot e:
              I was hoping i could do it in one line with a regular expression
              rather than creating a function that splits it and searches for
              specific things.
              >
              Anybody got any example regular expressions i could use?
              I am by no means a Regex expert - I am simply a man with Expresso.



              But the following may do what you need:

              //////////////////////////
              Imports System.Text.Reg ularExpressions

              Module Module1

              Sub Main()
              Dim dateString As String = "1w 2d 3h 15m"

              '// These will throw an InvalidCastExce ption if no match is
              found.
              '// You could use Regex.IsMatch or Integer.TryPars e to check
              '// the string value before casting into an integer.
              Dim week As Integer = CInt(Regex.Matc h(dateString, "(?<Week>
              \d{1,}(?=w))"). Value)
              Dim day As Integer = CInt(Regex.Matc h(dateString, "(?<Day>
              \d{1,}(?=d))"). Value)
              Dim hour As Integer = CInt(Regex.Matc h(dateString, "(?<Hour>
              \d{1,}(?=h))"). Value)
              Dim minute As Integer = CInt(Regex.Matc h(dateString, "(?
              <Minute>\d{1,}( ?=m))").Value)

              Console.WriteLi ne("The Week is {0}", week.ToString() )
              Console.WriteLi ne("The Day is {0}", day.ToString())
              Console.WriteLi ne("The Hour is {0}", hour.ToString() )
              Console.WriteLi ne("The Minute is {0}", minute.ToString ())

              Console.Read()
              End Sub

              End Module
              /////////////////////////

              Thanks,

              Seth Rowe

              Comment

              • Dan

                #8
                Re: Extracting numbers from a string

                That worked a treat, thanks very much.

                And thanks for pointing me in the direction of Expresso too, should
                prove very helpful in the future.

                Comment

                • Nick

                  #9
                  Re: Extracting numbers from a string

                  Dim myTimetable As Hashtable

                  Dim myHashtable As New Hashtable

                  myHashtable.Add ("Weeks", "w")

                  myHashtable.Add ("Days", "d")

                  myHashtable.Add ("Hours", "h")

                  myHashtable.Add ("Minutes", "m")

                  myTimetable = ParseTimeString (myHashtable, "1w 2d 3h 15m")

                  Debug.Print("We eks=" & myTimetable("We eks").ToString _

                  & " Days=" & myTimetable("Da ys").ToString _

                  & " Hours=" & myTimetable("Ho urs").ToString _

                  & " Minutes=" & myTimetable("Mi nutes").ToStrin g)

                  Function ParseTimeString (ByVal inHashtable As Hashtable, ByVal inString As
                  String) As Hashtable

                  Dim myRegex As System.Text.Reg ularExpressions .Regex

                  Dim myMatch As System.Text.Reg ularExpressions .Match

                  Dim myDictionaryEnt ry As DictionaryEntry

                  Dim Timetable As New Hashtable

                  For Each myDictionaryEnt ry In inHashtable

                  Dim myPattern As String

                  myPattern = "(?<" & myDictionaryEnt ry.Key & ">\d{1,}(?= " &
                  myDictionaryEnt ry.Value & "))"

                  myRegex = New System.Text.Reg ularExpressions .Regex(myPatter n)

                  myMatch = myRegex.Match(i nString)

                  If myMatch.Success = True Then

                  Timetable.Add(m yDictionaryEntr y.Key,
                  myMatch.Groups( myDictionaryEnt ry.Key).Value)

                  Else

                  Timetable.Add(m yDictionaryEntr y.Key, vbNull)

                  End If

                  Next

                  Return Timetable

                  End Function


                  Comment

                  • Cor Ligthert[MVP]

                    #10
                    Re: Extracting numbers from a string

                    Dan,
                    I was hoping i could do it in one line with a regular expression
                    rather than creating a function that splits it and searches for
                    specific things.
                    Why you want a slow solution instead of a quick one?

                    Cor

                    Comment

                    • Dan

                      #11
                      Re: Extracting numbers from a string

                      On Oct 6, 6:32 am, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
                      wrote:
                      Dan,
                      >
                      I was hoping i could do it in one line with a regular expression
                      rather than creating a function that splits it and searches for
                      specific things.
                      >
                      Why you want a slow solution instead of a quick one?
                      >
                      Cor
                      Are you saying that regular expressions are slower than looping
                      through an array of strings?

                      Comment

                      Working...