Reg python regexp

Paul Moore p.f.moore at gmail.com
Wed Mar 21 06:56:06 EDT 2018


Hi,
You don't need a regexp for this, the "replace" method on a string
will do what you want:

>>> s = 'this is a [string'
>>> print(s.replace('[', '\\['))
this is a \[string

Paul


On 21 March 2018 at 10:44,  <sankarramanv at gmail.com> wrote:
> Hi,
>
> I have a requirement.
>
> cmd="cat <file>|grep -c 'if [ -t 1 ]; then mesg n 2>/dev/null; fi'"
>
> I need to escape only the square brackets in above variable since its not grepping without escaping the brackets.
>
> Please help.
>
> Thanks.
> --
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list