Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
If I look at a string that can be any length and can contain any characters, is there an easy way of checking if there is a string of data within the main string that starts with "[" and ends with "]" and extract that text, with the square brackets included ?

For example :

This is data that I am not interested in - This is ignored
This data has an opening bracket [ so I'm not interested - This is ignored
This data has a closing bracket ] so I'm not interested - This is ignored
This data has [Look at me] in it so I want it - [Look at me] is extracted

Hope that is clear, it wasn't easy to explain !!!

I realise that I could use a loop to find an opening bracket & another to find a closing bracket & then using their positions, extract the data, but that seems long-winded ...
Posted
Updated 25-Oct-12 6:15am
v2

1 solution

Easiest way is to use a Regex:

\[.*?\]
will do it.
VB
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.
 
Share this answer
 
v2
Comments
Maciej Los 25-Oct-12 13:05pm    
Excellent! +5
Gary Heath 25-Oct-12 13:25pm    
That works, brilliant ... any chance you can explain what it's doing, how & why it works ?!?!?
OriginalGriff 25-Oct-12 14:17pm    
Answer updated
Gary Heath 25-Oct-12 18:19pm    
Magnificent reply, and yes, I can see how powerful that could be. As I mentioned in my original question, the obvious way to do something like this is looping & finding, looping & finding and it could take ages in comparison to something like what you did ... thank you very much for the answer & the explanation, I am truly impressed :-) !!!
OriginalGriff 26-Oct-12 3:32am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900