^(?!My Words).*$
^(?!
文字列の先頭、または複数行フラグが付いた行を探すことを意味し、その後に ... が続きません。
^
これが、アンカーを使用するときに「My Words」で始まる文が一致しない理由です。
y Words
アンカーを削除するときに が一致する理由は、 の後の位置に一致する が後に^
ない文字列内の任意のポイントを探しているだけだからです。My Words
M
My Words
文字列と がどのよう(?!My Words).*$
に適用されるかを見てみましょう。
これが文字列の開始点であることを覚えておいて^
ください。正規表現に入れていなくても、正規表現エンジンはその位置で開始されます。仕組みを少し単純化します。
正規表現エンジンの最初のステップ:
^My Words
|
Regex engine starts here, and looks if the current position onwards
matches (?!My Words), which it does not.
第二段階:
^My Words
|
Regex engine evaluates the 'M', and finds that from this position
onwards also fails to match (?!My Words)
3 番目のステップ:
^My Words
|
Standing at 'y', it finds that the lookahead now does not match 'My Words'.
This allows the rest of the pattern '.*$' to be applied, which matches
from 'y' till end of string.