Help with parsing between brackets

  • Thread starter Thread starter Vjay77
  • Start date Start date
V

Vjay77

Hi,
there is one more problem I'd need your help with.

I have log file with line like this one:


3/2/2004 12:09:31 AM - ( 52400) +OK Mailbox locked and ready
Code:
I need to extract the number between brackets, i this case it would be
:52400

Text outside of brackets might change so it could be easily something
like this:

[code]
3/12/2004 01:09:31 AM - ( 52400) Adding address to POP Before SMTP
manager
[code]

or like this:

[code]
3/2/2004 12:09:30 AM - ( 52400) START
[code]

Is there any way to extract the number between brackets?

Thanks a lot.
vjay
 
Hi Vjay,

You can use regulare expressions for this,

I do never use that, I do not like them

I think I would do it like this.

Dim a As String = "3/2/2004 12:09:31 AM - ( 52400) +OK Mailbox locked and
ready"
Dim b As String = a.Substring(a.IndexOf("(") + 1, a.IndexOf(")") -
a.IndexOf("(") - 1)

:-))

If you want you can also use "instr" but that is also not my favorite
although it is fast.

Cor
 
Thanks yet again.
Friend of mine finally wrote the function for extracting the text
between any two strings.
Check this out, it worked fine for me:


Function GetString(ByVal strIn As String, ByVal StartString As
String, ByVal EndString As String) As String
Try
Return Mid(strIn, InStr(1, strIn, StartString,
CompareMethod.Text) + 1, (InStr(1, strIn, EndString,
CompareMethod.Text) - InStr(1, strIn, StartString,
CompareMethod.Text)) - 1)
Catch objE As Exception
Return vbNullString
End Try
End Function


You can use it like this:

dim extract as string = GetString( "blahblah(123)blahblah" , "(" ,
")" )

Result is: 123

Nice, isn't it?

vjay


backup of this topic at:
http://www.dotnetboards.com/viewtopic.php?p=72512
 
Hi Vjay,

That is almost the same function only much worse, as that I have sended you
6 hours before.

Are you only posting and not looking?
Function GetString(ByVal strIn As String, ByVal StartString As
String, ByVal EndString As String) As String
Try
Return Mid(strIn, InStr(1, strIn, StartString,
CompareMethod.Text) + 1, (InStr(1, strIn, EndString,
CompareMethod.Text) - InStr(1, strIn, StartString,
CompareMethod.Text)) - 1)
Catch objE As Exception
Return vbNullString
End Try
End Function

This would be what you got from me in the same insecure situation as I now
see from the rows you are probably using as with your friend.
\\\
function GetString(byval a as string) as string
If a.IndexOf("(") <> -1 AndAlso a.IndexOf(")") <> -1 Then
return a.Substring(a.IndexOf("(") + 1, a.IndexOf(")") -a.IndexOf("(") -
1)
else
return ""
end function
///

But now I see it, I think that it is as as less save as from your friend,
because this means that you are not sure if those brackets are in it and
where they are in it and if there are more in it..

Cor
 
Back
Top