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 for 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:
Now consider your input,
AAstring:So the lookbehind is working, it just doesn’t do what you think it does. I think what you want is something like this:
That anchors itself to the beginning of the string, so you know where the lookahead is going to look. Here’s what it means:
You could also try
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.