0

I'm having a few problems with Regular Expressions in VB.NET.

I have a String like this one: "[Type=User][User=Hello]Thats the message"

I want to have the "Thats the message" part, so I thought that the best way to do that was replacing "[Type=User][User=Hello]" for "".

Notes:

  • "Thats the message" is always at the end of the String and it can be "" (without characters).

  • [Type=XXXX] can be anything, I mean, it can be [Type=Password], [Type=Message]...

So, here is what I did:

Dim regOptions As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.Singleline
Regex.Replace(buffer, "^(.*)[^\]]*$", "", regOptions)

It doesn't work, the string 'buffer' is not modified.

1
  • Surly you could use String.Split for this, using ] as the delimiter character? Commented Oct 2, 2012 at 12:47

1 Answer 1

4

According to the documentation of that overload it will return the resulting string (after all, in .Net strings are immutable).

So, instead, try:

buffer = Regex.Replace(buffer, "^(.*)[^\]]*$", "", regOptions)
Sign up to request clarification or add additional context in comments.

2 Comments

Right. Also, I don't think that pattern will work. Why not just \[.*\] ?
A better pattern might be \[([^\]]+)\], but the reason the replacement wasn't doing anything was that the result was never read...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.