|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
find number in a stringHello 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 "androoo" <a***@northwaves.com> wrote in news:1164034942.891789.106920 @m73g2000cwd.googlegroups.com:> Hello all, I would use regular expressions - you can specific the search pattern to > > 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 ? only search for numeric characters... like: [0-9](1,) which will search for 1 or more numeric characters. 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. 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. 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. 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. 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: Bits.Bytes.>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. -- http://bytes.thinkersroom.com
naming conventions forced by VS.NET?
Attribute instantiation How to declare a sub class thread dead lock Passing multiple arguments to the client-side JavaScript function in AJAX Can a Decimal variable be set to "Not a Number" I need 2-3 books on VB.NET, Office automation. Any suggestions? DataGridView1 (VS2005) Multi line grid row with .net compact framework 2.0 Search string in textfile |
|||||||||||||||||||||||