Match the last occurrence of delimiter in the expect function of pexpect
<p>I utilize pexpect for automation. The delimiter for the command prompt is '>'. However it so happens that sometimes the output itself contain a '>' sign causing the expect function to delimit at that precise location rather than taking all the output till the command prompt.
Is there any way to make expect function pick the last occurrence of the delimiter character?</p>

<p>Pexpect cannot tell which <code>&gt;</code> is the last one because the spawned process may keep outputting something. To solve your problem you can customize the shell prompt and then you just expect your own shell prompt. For example:</p>

<pre class="lang-none prettyprint-override"><code>[STEP 101] # cat pexp.py
import pexpect, sys

prompt = 'my-ugly-prompt'

ssh = pexpect.spawn('ssh localhost')
ssh.logfile_read = sys.stdout

ssh.expect('password:')
ssh.sendline('password')

ssh.expect('bash-')
ssh.sendline('PS1="[%s] # "' % prompt)
ssh.expect(prompt)

ssh.sendline('echo hello world')
ssh.expect(prompt)

ssh.sendline('exit')
ssh.expect(pexpect.EOF)
[STEP 102] # python pexp.py
root@localhost's password:
bash-4.4# PS1="[my-ugly-prompt] # "
[my-ugly-prompt] # echo hello world
hello world
[my-ugly-prompt] # exit
exit
Connection to localhost closed.
[STEP 103] #
</code></pre>

python shell automation command-line-interface pexpect