.NET Regex negative lookbehind not working
I am trying to match the following text (anywhere in the string) where string can be anything between A and ;
Astring;
However I don't want to match the following
aAstring;
AAstring;
The expression (?<![A|a])A.*?;
works ok for aAstring; but not f开发者_运维技巧or AAstring;
It seems that the lookaround doesn't work for the same character? I must be missing something simple here.
First, that's a lookbehind, not a lookahead. To understand what's going on here, break down what the regex is saying:
(?<![A|a])A #An A that is not preceded by A, a, or a literal |
.?; #Any number of characters followed by ;
Now consider your input, AAstring
:
A #An A that is not preceded by A (because it's at the beginning of the string)
Astring #Some characters followed by ;
So the lookbehind is working, it just doesn't do what you think it does. I think what you want is something like this:
^(?![Aa]{2})A.*?;
That anchors itself to the beginning of the string, so you know where the lookahead is going to look. Here's what it means:
^ #Beginning of the line
(?![Aa]{2}) #"The following string must not start with two As in a row, regardless of case"
A.*?; #Capital A followed by anything followed by ;
You could also try
\b(?![Aa]{2})A.*?;
if your target isn't at the very beginning of the line.
Note that this is actually the same as ^A(?![Aa]).*?;
or \bA(?![Aa]).*?;
, but the above might be easier to understand.
If the string
part can be any string, then it matches correctly. The A
in the regex matches with the first A
in the text and .*?
matches with Astring
. And there is no A
or a
before the first A
.
If you have some rules for the string
part, then you should add them and it should fix your problem.
The negative look-behind is working correctly. To achieve what you're attempting I suggest using a negative look-ahead, as shown in this pattern: "A(?![Aa]).*?;"
It will match A
then fail to match if the next character is either A
or a
. If you want to ensure the string begins with A
rather than being a partial match within a larger string, add the \b
metacharacter to the beginning of the pattern: @"\bA(?![Aa]).*?;"
精彩评论