find number in a string

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

    #1

    find number in a string

    Hello all,

    I have a string for example :

    strTest = "a lineof text (60) witha number in it"

    I need to extract the number from between the brackets, the postion of
    the number in brackets is never the same...

    So in the above example i need to extract 60

    Anyone help on how to do this the simplest way ?

    Thanks

    andy

  • Spam Catcher

    #2
    Re: find number in a string

    "androoo" <andy@northwave s.comwrote in news:1164034942 .891789.106920
    @m73g2000cwd.go oglegroups.com:
    Hello all,
    >
    I have a string for example :
    >
    strTest = "a lineof text (60) witha number in it"
    >
    I need to extract the number from between the brackets, the postion of
    the number in brackets is never the same...
    >
    So in the above example i need to extract 60
    >
    Anyone help on how to do this the simplest way ?
    I would use regular expressions - you can specific the search pattern to
    only search for numeric characters... like:

    [0-9](1,) which will search for 1 or more numeric characters.

    Comment

    • androoo

      #3
      Re: find number in a string

      thanks, im a bit new to regular expressions but will have a go

      Spam Catcher wrote:
      "androoo" <andy@northwave s.comwrote in news:1164034942 .891789.106920
      @m73g2000cwd.go oglegroups.com:
      >
      Hello all,

      I have a string for example :

      strTest = "a lineof text (60) witha number in it"

      I need to extract the number from between the brackets, the postion of
      the number in brackets is never the same...

      So in the above example i need to extract 60

      Anyone help on how to do this the simplest way ?
      >
      I would use regular expressions - you can specific the search pattern to
      only search for numeric characters... like:
      >
      [0-9](1,) which will search for 1 or more numeric characters.

      Comment

      • FishingScout

        #4
        Re: find number in a string


        This can work but it is incredibly weak:

        Dim strTest As String = "a lineof text (60) witha number in it"

        Dim Results As String() = strTest.Split(N ew Char() {"("c,
        ")"c}, 3)

        If Results.Length = 3 Then
        Console.Write(" I found " & Results(1) & vbCrLf)
        End If


        androoo wrote:
        thanks, im a bit new to regular expressions but will have a go
        >
        Spam Catcher wrote:
        "androoo" <andy@northwave s.comwrote in news:1164034942 .891789.106920
        @m73g2000cwd.go oglegroups.com:
        Hello all,
        >
        I have a string for example :
        >
        strTest = "a lineof text (60) witha number in it"
        >
        I need to extract the number from between the brackets, the postion of
        the number in brackets is never the same...
        >
        So in the above example i need to extract 60
        >
        Anyone help on how to do this the simplest way ?
        I would use regular expressions - you can specific the search pattern to
        only search for numeric characters... like:

        [0-9](1,) which will search for 1 or more numeric characters.

        Comment

        • androoo

          #5
          Re: find number in a string

          thanks for your help :)
          I will try it out

          FishingScout wrote:
          This can work but it is incredibly weak:
          >
          Dim strTest As String = "a lineof text (60) witha number in it"
          >
          Dim Results As String() = strTest.Split(N ew Char() {"("c,
          ")"c}, 3)
          >
          If Results.Length = 3 Then
          Console.Write(" I found " & Results(1) & vbCrLf)
          End If
          >
          >
          androoo wrote:
          thanks, im a bit new to regular expressions but will have a go

          Spam Catcher wrote:
          "androoo" <andy@northwave s.comwrote in news:1164034942 .891789.106920
          @m73g2000cwd.go oglegroups.com:
          >
          Hello all,

          I have a string for example :

          strTest = "a lineof text (60) witha number in it"

          I need to extract the number from between the brackets, the postion of
          the number in brackets is never the same...

          So in the above example i need to extract 60

          Anyone help on how to do this the simplest way ?
          >
          I would use regular expressions - you can specific the search pattern to
          only search for numeric characters... like:
          >
          [0-9](1,) which will search for 1 or more numeric characters.

          Comment

          • Rad [Visual C# MVP]

            #6
            Re: find number in a string

            Hey Androo,

            Try this code:

            '
            ' Create a regex object
            '
            dim myRegex as new
            System.Text.Reg ularExpressions .Regex("(?<numb er>\d+)")
            '
            ' Collect our input string
            '
            dim myInputString as string = "a lineof text (60) witha number in it"
            '
            ' Capture our input
            '
            dim myNumber as string =
            myRegex.Match(m yInputString).G roups("number") .Value
            '
            ' Use it as we will
            '
            Console.WriteLi ne(myNumber)

            On 21 Nov 2006 11:47:55 -0800, "androoo" <andy@northwave s.comwrote:
            >thanks, im a bit new to regular expressions but will have a go
            >
            >Spam Catcher wrote:
            >"androoo" <andy@northwave s.comwrote in news:1164034942 .891789.106920
            >@m73g2000cwd.g ooglegroups.com :
            >>
            Hello all,
            >
            I have a string for example :
            >
            strTest = "a lineof text (60) witha number in it"
            >
            I need to extract the number from between the brackets, the postion of
            the number in brackets is never the same...
            >
            So in the above example i need to extract 60
            >
            Anyone help on how to do this the simplest way ?
            >>
            >I would use regular expressions - you can specific the search pattern to
            >only search for numeric characters... like:
            >>
            >[0-9](1,) which will search for 1 or more numeric characters.
            --

            Bits.Bytes.

            Comment

            • FishingScout

              #7
              Re: find number in a string

              Androoo,

              You should use Rad's solution.


              androoo wrote:
              thanks for your help :)
              I will try it out
              >
              FishingScout wrote:
              This can work but it is incredibly weak:

              Dim strTest As String = "a lineof text (60) witha number in it"

              Dim Results As String() = strTest.Split(N ew Char() {"("c,
              ")"c}, 3)

              If Results.Length = 3 Then
              Console.Write(" I found " & Results(1) & vbCrLf)
              End If


              androoo wrote:
              thanks, im a bit new to regular expressions but will have a go
              >
              Spam Catcher wrote:
              "androoo" <andy@northwave s.comwrote in news:1164034942 .891789.106920
              @m73g2000cwd.go oglegroups.com:

              Hello all,
              >
              I have a string for example :
              >
              strTest = "a lineof text (60) witha number in it"
              >
              I need to extract the number from between the brackets, the postion of
              the number in brackets is never the same...
              >
              So in the above example i need to extract 60
              >
              Anyone help on how to do this the simplest way ?

              I would use regular expressions - you can specific the search pattern to
              only search for numeric characters... like:

              [0-9](1,) which will search for 1 or more numeric characters.

              Comment

              Working...