find number in a string

  • Thread starter Thread starter androoo
  • Start date Start date
A

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
 
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, im a bit new to regular expressions but will have a go

Spam said:
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

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

Spam said:
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
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

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

Spam said:
@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)

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

Spam said:
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.

thanks for your help :)
I will try it out
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

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

Spam Catcher wrote:
@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.
 
Back
Top