Regex and Match classes

  • Thread starter Thread starter Christoph Boget
  • Start date Start date
C

Christoph Boget

I'm trying to grab everything between the square brackets
and this code just isn't working. I'm not sure what's going
on. I've tested the regular expression elsewhere and it works
as expected. Just not in my C# code...

Regex oRegex = new Regex( @"\[([\S\s]+)\]" );

string sSelectedItem = "[joe#bob!briggs@9]";

Match oMatch = oRegex.Match( sSelectedItem );

if( oMatch.Success ) {
MessageBox.Show( "Matched! Value is " + oMatch.Value );
sSelectedItem = oMatch.Value;

}
MessageBox.Show( "You just selected " + sSelectedItem );

What am I doing wrong?
 
Christoph Boget said:
I'm trying to grab everything between the square brackets
and this code just isn't working. [...]

Regex oRegex = new Regex( @"\[([\S\s]+)\]" );
string sSelectedItem = "[joe#bob!briggs@9]";
Match oMatch = oRegex.Match( sSelectedItem );

Is that your exact code? It works for me and produces the success
message.

Incidentally, instead of [\S\s] (meaning "any character that is or
isn't whitespace"), you could just use a single dot (meaning "any
character").

P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top