Home All Groups Group Topic Archive Search About

find number in a string

Author
20 Nov 2006 3:02 PM
androoo
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

Author
20 Nov 2006 3:32 PM
Spam Catcher
"androoo" <a***@northwaves.com> wrote in news:1164034942.891789.106920
@m73g2000cwd.googlegroups.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.
Author
21 Nov 2006 7:47 PM
androoo
thanks, im a bit new to regular expressions but will have a go

Spam Catcher wrote:
Show quoteHide quote
> "androoo" <a***@northwaves.com> wrote in news:1164034942.891789.106920
> @m73g2000cwd.googlegroups.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.
Author
22 Nov 2006 8:47 AM
FishingScout
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(New Char() {"("c,
")"c}, 3)

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


androoo wrote:
Show quoteHide quote
> thanks, im a bit new to regular expressions but will have a go
>
> Spam Catcher wrote:
> > "androoo" <a***@northwaves.com> wrote in news:1164034942.891789.106920
> > @m73g2000cwd.googlegroups.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.
Author
22 Nov 2006 10:28 AM
androoo
thanks for your help :)
I will try it out

FishingScout wrote:
Show quoteHide quote
> 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(New 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" <a***@northwaves.com> wrote in news:1164034942.891789.106920
> > > @m73g2000cwd.googlegroups.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.
Author
24 Nov 2006 7:10 PM
FishingScout
Androoo,

You should use Rad's solution.


androoo wrote:
Show quoteHide quote
> 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(New 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" <a***@northwaves.com> wrote in news:1164034942.891789.106920
> > > > @m73g2000cwd.googlegroups.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.
Author
22 Nov 2006 7:39 PM
Rad [Visual C# MVP]
Hey Androo,

Try this code:

'
' Create a regex object
'
dim myRegex as new
System.Text.RegularExpressions.Regex("(?<number>\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(myInputString).Groups("number").Value
'
' Use it as we will
'
Console.WriteLine(myNumber)

Show quoteHide quote
On 21 Nov 2006 11:47:55 -0800, "androoo" <a***@northwaves.com> wrote:

>thanks, im a bit new to regular expressions but will have a go
>
>Spam Catcher wrote:
>> "androoo" <a***@northwaves.com> wrote in news:1164034942.891789.106920
>> @m73g2000cwd.googlegroups.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.
http://bytes.thinkersroom.com