简体   繁体   中英

Regex Nested Quantifier *

I am trying to use Regex to make a comment finder in text, but I get the error 'Nested Quantifier *'

Regex Comment = new Regex("/*.*?*/");

As in: It starts at /* and ends at */ [Comments]

What is the problem with doing this?

Its because literal * must be escaped \\* . Otherwise the regex engine take it for the quantifier * (zero or more time). The error message you obtain is relative to *?* (the regex engine see two consecutive quantifiers *? and * )

You can write your pattern like this:

Regex Comment = new Regex(@"(?s)/\*.*?\*/");

(I added the (?s) modifier to allow the dot to match newlines)

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