0

I am using lib pexpect to wait for the prompt line:

'[root@%s]#' % (x)

where x is a machine name "ABC-EFG-0001", which contains hyphen "-" and therefore the bad character error in regex. How do I make it to take the hyphen as it is (to take the machine name)? I've checked if the hyphen is removed then error is gone.

P.S These script used to work (in 2015), without any change now gives bad character range error, was there anychange in re.py?

the error msg:

File "/projects/hnd_tools/python/pexpect-0.99/pexpect.py", line 623, in expect
    compiled_pattern_list = self.compile_pattern_list(pattern)
File "/projects/hnd_tools/python/pexpect-0.99/pexpect.py", line 557, in compile_pattern_list
    compiled_pattern_list.append(re.compile(p, re.DOTALL))
File "/usr/lib/python2.7/re.py", line 190, in compile
    return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 245, in _compile
    raise error, v # invalid expression
error: bad character range

thanks.

apcircle
  • 3
  • 4
  • I am not familiar with pexpect, but if it indeed uses Python `re`, then escaping the special characters should work: `"ABC-EFG-0001".replace("-","\\-")` – DYZ Aug 27 '17 at 05:17
  • It happened because `[...]` creates a character class. You need to escape `[` - `r'\[root@%s]#' % (x)` – Wiktor Stribiżew Aug 27 '17 at 08:13

0 Answers0