简体   繁体   中英

How can i match this text with a regex?

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

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

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;-).

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>\*/)

在打开正则表达式的“多行”选项后,您的正则表达式应与您指定的所有文本匹配。

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM