开发者

Regular Expression Pattern Error C#

开发者 https://www.devze.com 2023-01-20 17:34 出处:网络
WhenI haveanExpression declared like someText = Regex.Replace(someText, @\"/*.*?*/\", \"\"); The Error Says

When I have an Expression declared like

 someText = Regex.Replace(someText, @"/*.*?*/", "");

The Error Says System.ArgumentException: par"/*.*?*/" parsing - Nested quantifier开发者_StackOverflow中文版 *.

How to rewrite the code to avoid this error?


It doesn't like that you have this: ?*

This basically translates to "zero or one of the previous expression zero or more times" which seems a little odd. I'm pretty sure that's the same thing as saying "zero or more times". Can you explain what you are trying to do in more detail?

I suspect that if you change your regex to this it will do what you want:

(/*.*)*/


Maybe what is needed is a verbal description or sample of what you are trying to match. Here is my guess of what you want. I just added an escape for the "?" character.

string someText = Regex.Replace(someText, @"/*.*\?*/", "");


It appears you're trying to parse /* */ style comments. You may wish to try a regex like:

someText = Regex.Replace(someText, @"/\*.*\*/", "");

This ensures that your * are escaped as actual characters.


Here is a good site to test your regular expressions without much trouble:

http://www.regular-expressions.info/javascriptexample.html

I hope this will help a bit.

0

精彩评论

暂无评论...
验证码 换一张
取 消