Cadburry
Cadburry

Reputation: 1864

Regex from special characters "/*" to "*/"

I'm quite new with regex. (.net) I need a regular expression which selects anything from /* to */ (including /* and */). My Problem - is that i'm not able to get the right expressen with these special characters. With standard strings it works fine. Once more... it should select over linebreaks. i.e.:

text
/*
some text
*/
text

the result should be:

/*
some text
*/

Can someone please help me?

Thanks a lot!

Upvotes: 0

Views: 66

Answers (2)

Jonny
Jonny

Reputation: 2927

this should work: Regex.Match("/*hello*/", "^(/\*(.*?)\*/)$", RegexOptions.SingleLine)

Upvotes: 2

polkduran
polkduran

Reputation: 2551

You need to escape the * : /\*.*?\*/ And use the Singleline option so the . (dot) matches break lines.

You can access the matched content like this :

string value;
string pattern = @"/\*.*?\*/"
Match match = Regex.Match(input, pattern, RegexOptions.Singleline);
if( match.Success )
{
  value = match.Value;
}

Upvotes: 1

Related Questions