Easiest way is to use a Regex:
\[.*?\]
will do it.
Public Dim regex As Regex = New Regex("\[.*?\]")
...
Dim m As Match = regex.Match(InputText)
If m.Success Then
Dim matched As String = m.Value
...
End If
"That works, brilliant ... any chance you can explain what it's doing, how & why it works ?!?!?"
Whoo boy! Now there is a *BIG* question! :laugh:
Regular Expressions (or Regex for short) are text based pattern matchers - they look for specified patterns within strings and can perform complicated manipulations on them.
I won't even try to go into the full details here - it would take me all night to type even half of them - but the actual expression is pretty simple:
\[.*?\]
Is the pattern the matcher is looking for, and it's very, very simple as Regexes go.
First off, the match is a normal string, but nearly every character means something.
The first thing to note is the '\' is a special character which says "the next character is not a special character, it is a literal so leave it alone". Which means that '\[' is a single literal '[' character (and '\]' is a single ']') - we need to include the '\' before the '[' because square brackets have a special meaning in Regexes.
Next we have a '.' - which is Regex for "any character at all".
Followed by a '*' - which means "any number (including zero) of whatever is immediately left of me"
Then a '?' which modifies the meaning of the '*' by adding on the end "...but as few as possible"
So the pattern it is looking for is:
\[ . * ? \]
An open square bracket followed by any number of any character ending with the first instance of a close square bracket.
Which is what you were looking to extract from your string.
Regexes can get a lot more complex: You can name parts of it, and do "any of the characters in this list" (which is what the square brackets do if you don't put the back slash in front of them) or specify a number of character between 3 and 9, and a huge range of other things.
They are well worth learning about; for the tasks they are suited to they are incredibly powerful and can save a huge amount of slow code. The down side of this is that they are a pain to maintain and read, particularly when you are getting started! :laugh:
If you want to play with them (and it's a good idea) get a copy of
Expresso [
^] - it's free, and it examines and generates Regular expressions. I use it a lot for testing, and I wish I'd written it!
There are whole books devoted to what you can do with regexes, but there is a tutorial here
The 30 Minute Regex Tutorial[
^] which will explain a good amount about them.