If you just need to get true or false, you may put the s*
into the lookahead:
Regex.IsMatch(s, @"A(?!s*B)")
It finds A
that has no 0+ whitespaces followed with B
after it.
See the regex demo.
In your pattern, As*(?!B)
, the negative lookahead can be executed after any 0+ whitespaces, and once a whitespace not followed with B
is found, a valid match is returned (that happens due to backtracking that is possible thanks to s*
quantified pattern).
If you need to actually match the A
and the whitespace after it, but if these whitespaces are not followed with B
, use the pattern from my comment.
(?>As*)(?!B)
This pattern matches:
(?>As*)
- an atomic group, matches A
, then 0+ whitespaces with no backtracking into the group pattern allowed
(?!B)
- no B
after the spaces, or the whole match is failed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…