Firstly, it is important to understand that the characters '^' and '$' anchor the beginning and end of received data. They do NOT anchor the beginning and end of a line.
To understand the observed behavior let's
assume there is a "RegExBuffer" for the incoming data. Since you have defined two characters in your RegEx this buffer will hold up to two bytes. This buffer will be emptied as soon as it does not match the RegEx. Let's have a closer look at what happens when you type chess:
Our RegEx is
"s\r".
'c' is typed
Code:
RegExBuffer [c][] --> RegEx is mismatched and the buffer is emptied
-> RegExBuffer [][]
The above procedure is repeated for the characters
'h' and
'e'.
Now the first
's' is typed
Code:
RegExBuffer [s][] --> RegEx is starting to match; the buffer is not emptied
-> RegExBuffer [s][]
Now the second
's' is typed
Code:
RegExBuffer [s][s] --> RegEx is mismatched and the buffer is emptied
-> RegExBuffer [][]
If you hit
'return' at this point then the buffer would look like
Code:
RegExBuffer [\r][] --> RegEx is mismatched and the buffer is emptied
-> RegExBuffer [][]
If you do NOT hit
'return' after the second
's', but misspell chesss and type in a third
's' then this is how RegExBuffer looks like
Code:
RegExBuffer [s][] --> RegEx is starting to match again; the buffer is not emptied
-> RegExBuffer [s][]
If you hit
'return' after the third
's' then
Code:
RegExBuffer [s][\r] --> This is a perfect match! The 'Exiting' branch is executed!
From this example it is hopefully clear that any odd number of
consecutive 's' will match the RegEx, i.e. ches, chesss and also chesssss (that's 5 s) will trigger the
'Exiting' branch.
To further verify the mechanism I described above you can change your RegEx to
"ss\r". Now the RegEx buffer will hold up to 3 characters. Hence, the
'Exiting' branch will be executed whenever you enter 2,5,8 ... consecutive
's'.
I am quite confident that there is a workaround for this problem; although not sure how it will look. It would be nice if you could post the solution here once you have it.