简体   繁体   English

我该如何将此文本与正则表达式匹配?

[英]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. 该表达式在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>\\*/ 尝试以下正则表达式: /\\*<parameters>\\*/(?<value>[^/]*)/\\*</parameters>\\*/

A good tool for fooling around with real c# Regex patterns is regex-freetool on code.google.com 真正的C#正则表达式模式的一个很好的工具是code.google.com上的regex-freetool

RegexOptions.Multiline only changes the semantics of ^ and $ -- you need RegexOptions.Singleline to let . RegexOptions.Multiline仅更改^$的语义-您需要RegexOptions.Singleline来使. 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). 如Alex所述,您可以使用Singleline修饰符让点匹配换行符(\\ r和\\ n)。 You can also use the inline form - (?s) - to specify it within the regex itself: 您还可以使用内联形式- (?s) -在正则表达式本身中指定它:

(?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. 否则,您将从第一个<parameters>标记匹配到最后一个</parameters>标记。

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 . 我不想给你这个问题,所以您可能想尝试一下Ultrapico的免费工具(已注册),称为Expresso

I have stumbled with Regex for several time, and Expresso saved the day on all occassion. 我在Regex上跌跌撞撞已经有好几次了,而Expresso在所有情况下都节省了一天的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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