Regular Expression Syntax Problem

hammerman

Newcomer
Joined
Jan 16, 2004
Messages
17
okay.. I'm not the best at writing reg exp so I'm sure this is simple but I've tried it a hundred different ways... Given a string I want to pull out the first occurance of all characters,digits,spaces and special chars between the [ and ]
for instance I usally have a string such as "[blah boo & foo] + [heeee]" I want to return "[blah boo & foo]" below is my feable attempt to get this...

Dim r As Regex = New Regex("\[.*\]")
Dim m As Match = r.Match(stringGoesHere)
Dim basdf As String = m.Value

"\[.*\]" is somewhat sucessful.. but it returns the whole string (obviously because the string starts and ends with [ ]. So I try something like \[\w*\s*\] and that only works for something like [booo] no [boo foo]. I guess I'm stuck in the middle portion where I want everything between the [].. any help is much appreciated...
 
Try
Code:
Pattern = "((?<=\[)[^\[\]]*)"
The (?<=\[) skips until it finds a [ ( \[ )
The [^\[\]] picks up characters after the [ that is not a [ or ].
( \[ , \] )

Also, you can make your own little app that will test your RegEx expressions... you only need two TextBoxes and a Button :).
 
Works like a charm!

thanks - that works like a charm!!! One thing I love about reg exp is how elegant it is to write a complicated match pattern.. just wish I was better at doing them :D
 
Iceplug said:
Also, you can make your own little app that will test your RegEx expressions... you only need two TextBoxes and a Button :).


Iceplug ... you have any code for this test app? I think it would be a good way for me to learn regular expressions. Thanks...
 
I think IcePlug was simply suggesting that you create a project with a form that contains something like a few textboxes and button. Enter text to be searched into TextBox1, enter a search pattern in TextBox2, and when the user clicks the button, output the results in TextBox3.
 
Back
Top