ZitroTech ZitroTech - 4 years ago 198
Vb.net Question

Remove text between two tags

I'm trying to remove some text between two tags [ & ]

[13:00:00]


I want to remove 13:00:00 from [] tags.
This number is not the same any time.
Its always a time of the day so, only Integer and : symbols.

Someone can help me?

UPDATE:
I forgot to say something. The time (13:00:00) was picked from a log file. Looks like that:

[10:56:49] [Client thread/ERROR]: Item entity 26367127 has no item?!
[10:57:25] [Dbutant] misterflo13 : ils coute chere les enchent aura de feu et T2 du spawn??*
[10:57:35] [Amateur] firebow ?.SkyLegend.? : ouai 0
[10:57:38] [Novice] iPasteque : ils sont gratuit me
[10:57:41] [Novice] iPasteque : ils sont gratuit mec *
[10:57:46] [Dbutant] misterflo13 : on ma dit k'ils etait payent :o
[10:57:57] [Novice] iPasteque : on t'a mytho alors


Ignore the other text I juste want to remove the time between [ & ] (need to looks like []. The time between [ & ] is updated every second.

Answer Source

This is another possibility using Regex:

Public Function ReplaceTime(ByVal Input As String) As String
    Dim m As Match = Regex.Match(Input, "(\[)(\d{1,2}\:\d{1,2}(\:\d{1,2})?)(\])(.+)")
    Return m.Groups(1).Value & m.Groups(4).Value & m.Groups(5).Value
End Function

It's more of a readability nightmare but it's efficient and it takes only the brackets containing a time value.

I also took the liberty of making it match for example 13:47 as well as 13:47:12.

Test: http://ideone.com/yogWfD

(EDIT) Multiline example:

You can combine this with File.ReadAllLines() (if that's what you prefer) and a For loop to get the replacement done.

Public Function ReplaceTimeMultiline(ByVal TextLines() As String) As String
    For x = 0 To TextLines.Length - 1
        TextLines(x) = ReplaceTime(TextLines(x))
    Next
    Return String.Join(Environment.NewLine, TextLines)
End Function

Above code usage:

Dim FinalT As String = ReplaceTimeMultiline(File.ReadAllLines(<file path here>))

Another multiline example:

Public Function ReplaceTimeMultiline(ByVal Input As String) As String
    Dim ReturnString As String = ""
    Dim Parts() As String = Input.Split(Environment.NewLine)
    For x = 0 To Parts.Length - 1
        ReturnString &= ReplaceTime(Parts(x)) & If(x < (Parts.Length - 1), Environment.NewLine, "")
    Next
    Return ReturnString
End Function

Multiline test: http://ideone.com/nKZQHm

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download