tags:

views:

142

answers:

6

Hi all,

I've tried to make a regex expression to match a piece of code, but no success. The expression doesn't work in vs2008.

I've created this one:

/\*<parameters>\*/(?<value>[\r\n]*.*)/\*</parameters>\*/

the source to match is:

/*<parameters>*/ 
@parameter blue
,@parameter2 red
,@parameter3 green
,@parameter4 yellow /*</parameters>*/

Or better:

/*<parameters>*/  \r\n @parameter blue \r\n ,@parameter2 red \r\n ,@parameter3 green \r\n ,@parameter4 yellow /*</parameters>*/

Can anybody help me?

thanks, Rodrigo Lobo

A: 

Your regex should match the all the text you specified, after you turn on the regex "Multiline" option.

James
Regex regex = new Regex(@"/\*<parameters>\*/(?<value>[\r\n]*.*)/\*</parameters>\*/", RegexOptions.Multiline); Match match = regex.Match(scriptText); return match.Successit returns false..
Rodrigo Lobo
"Singleline" is the option he needs, not Multiline. And the "[\r\n]*" part isn't needed.
Alan Moore
+1  A: 

The following will do the trick:

/\*<parameters>\*/(.|\r|\n)*/\*</parameters>\*/

Alternatively, if you want to exclude the outer tokens from the match itself:

(?<=/\*<parameters>\*/)(.|\r|\n)*(?=/\*</parameters>\*/)
Joe Albahari
+2  A: 

RegexOptions.Multiline only changes the semantics of ^ and $ -- you need RegexOptions.Singleline to let . match line-ends as well (a confusion I've been caught in myself;-).

Alex Martelli
A: 

I don't want to give you fish for this question, so you may want to try out this free tool (with registration) from Ultrapico called Expresso.

I have stumbled with Regex for several time, and Expresso saved the day on all occassion.

Adrian Godong
+4  A: 

Try this Regex out: /\*<parameters>\*/(?<value>[^/]*)/\*</parameters>\*/

A good tool for fooling around with real c# Regex patterns is regex-freetool on code.google.com

Mazrick
A: 

As Alex said, you can use the Singleline modifier to let the dot match newline characters (\r and \n). You can also use the inline form - (?s) - to specify it within the regex itself:

(?s)/*<parameters>*/(?<value>.*?)/*</parameters>*/
Also notice the reluctant quantifier: ".*?". That's in case there's more than one potential match in the text; otherwise you would match from the first <parameters> tag to the last </parameters> tag.

Alan Moore