Find a string between 1 or 2 sets of parenthesis

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi,
I'm looking for a way in VB to find a string between two characters,
"(" and ")".

For example, for the string...

"THIS IS (ONE) AND THIS IS (TWO)"

....I would like for a variable to store the characters between the
second set of parenthesis, e.g. strMyString = "TWO".

But if the string to search only contains one set of parenthesis, to
store this instead. e.g.

strFirstString = "THIS IS (ONE)"
strMyString = "ONE"


Many thanks in advance.

Paul
 
Hi,
I'm looking for a way in VB to find a string between two characters,
"(" and ")".

For example, for the string...

"THIS IS (ONE) AND THIS IS (TWO)"

...I would like for a variable to store the characters between the
second set of parenthesis, e.g. strMyString = "TWO".

But if the string to search only contains one set of parenthesis, to
store this instead. e.g.

strFirstString = "THIS IS (ONE)"
strMyString = "ONE"

Assuming you are using VB.NET, then you need to look into the vaious
methods of the string class, notably:

http://msdn.microsoft.com/en-us/library/system.string.indexof.aspx

http://msdn.microsoft.com/en-us/library/system.string.lastindexof.aspx

http://msdn.microsoft.com/en-us/library/system.string.substring.aspx

and perhaps others.

Sorry, I ain'y gonna write the code for you. :-)
 
Assuming you are using VB.NET, then you need to look into the vaious
methods of the string class, notably:

http://msdn.microsoft.com/en-us/library/system.string.indexof.aspx

http://msdn.microsoft.com/en-us/library/system.string.lastindexof.aspx

http://msdn.microsoft.com/en-us/library/system.string.substring.aspx

and perhaps others.

Sorry, I ain'y gonna write the code for you. :-)



Thanks for the tips joecool.

I've managed to get it working. It's not perfect by any means but it
does the job.

Dim s1 As String = "Some (text). Some (more) nice text."
Dim i1 As Integer = s1.LastIndexOf("(") + 1
Dim strResult As String = s1.Substring(i1)
Dim IndexOfCloseParenthesis As Integer = strResult.LastIndexOf
(")")
TextBox1.Text = Microsoft.VisualBasic.Left(strResult,
IndexOfCloseParenthesis)

Textbox1.Text always shows the contents of the last set of
parenthesis, in this case "more".

Many thanks
Paul
 
Hi,
I'm looking for a way in VB to find a string between two characters,
"(" and ")".

For example, for the string...

"THIS IS (ONE) AND THIS IS (TWO)"

...I would like for a variable to store the characters between the
second set of parenthesis, e.g. strMyString = "TWO".

But if the string to search only contains one set of parenthesis, to
store this instead. e.g.

strFirstString = "THIS IS (ONE)"
strMyString = "ONE"


Many thanks in advance.

Paul

Dim example As String = "THIS IS (ONE) AND THIS IS (TWO)"
For Each m As Match In Regex.Matches(example, "\((.*?)\)")
Console.WriteLine (m.Groups(1).Value)
Next

HTH
 
Dim example As String = "THIS IS (ONE) AND THIS IS (TWO)"
For Each m As Match In Regex.Matches(example, "\((.*?)\)")
  Console.WriteLine (m.Groups(1).Value)
Next

HTH


Thanks Tom, that also works but how would I just work with the 2nd
match?
 
Thanks Tom, that also works but how would I just work with the 2nd
match?

The Matches method returns a collection... So, you could do something like
(air-code):

Dim ms As MatchCollection = Regex.Matches(example,....)
Console.WriteLine(ms(1).Groups(1).Value)

Of course, you might want to do error checking :)
 
Hello efc,
Thanks Tom, that also works but how would I just work with the 2nd
match?

You could get the requested result like this:

Dim s As String = "THIS IS (ONE) AND THIS IS (TWO)"
Dim sArr() As String = Split(s, "(")
Dim i As Integer
Dim iPosi As Integer

For i = UBound(sArr) To 0 Step -1
iPosi = InStr(sArr(i), ")")
If iPosi > 0 Then
MsgBox(Strings.Left(sArr(i), iPosi - 1))
Exit For
End If
Next



Best regards,

Martin
 
OP has not indicated whether bracketed text within a word is a success or
not. eg
"THIS IS (ONE) AND THIS IS(NOT)"

If only whole words count then your code can be simplified :
Dim a() = Split("THIS IS (ONE) AND THIS IS (TWO)", " ")
For N As Integer = 0 To A.count - 1
If a(N).StartsWith("(") And a(N).EndsWith(")") Then MsgBox(a(N))
Next
 
Back
Top