You're running into catastrophic backtracking:
Let's simplify the regex a bit (without the escaped quotes and without the second optional group because, as in your comment, it's irrelevant for the tested strings):
"(([^"]*))*"
([^"]*)
matches any string except quotes or backslashes. This again is enclosed in an optional group that can repeat any number of times.
Now for the string "ABC
, the regex engine needs to try the following permutations:
"
, ABC
"
, ABC
, <empty string>
"
, AB
, C
"
, AB
, C
, <empty string>
"
, AB
, <empty string>
, C
"
, AB
, <empty string>
, C
, <empty string>
"
, <empty string>
, AB
, C
"
, <empty string>
, AB
, C
, <empty string>
"
, <empty string>
, AB
, <empty string>
, C
, <empty string>
"
, <empty string>
, AB
, <empty string>
, C
"
, A
, BC
"
, A
, BC
, <empty string>
"
, A
, <empty string>
, BC
"
, <empty string>
, A
, BC
- etc.
"
, A
, B
, C
"
, A
, B
, C
, <empty string>
"
, A
, B
, <empty string>
, C
- etc. etc.
each of which then fails because there is no following "
.
Also, you're only testing for substrings instead of forcing the regex to match the entire string. And you usually want to use verbatim strings for regexes to cut down on the number of backslashes you need. How about this:
foundMatch = Regex.IsMatch(subjectString,
@"A # Start of the string
"" # Match a quote
(?: # Either match...
\. # an escaped character
| # or
[^""] # any character except backslash or quote
)* # any number of times
"" # Match a quote
# End of the string",
RegexOptions.IgnorePatternWhitespace);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…