Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
786 views
in Technique[技术] by (71.8m points)

c# - Regular expression with optional part and negative lookahead

I need check if a string contains the pattern: starts with "A" followed by zero or more spaces and then anything but not "B".

So, the following must match: "A". "AX", "A X", "A ", "A XB"

The following strings must not match: "AB", "A B"

My naive attempt was As*(?!B), but it matches the undesirable "A B".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...