0

Suppose such a list,

l = ['\\number', '\\A', '\\b', '\\B', '\\d', '\\D', '\\s', '\\S', '\\w', '\\W', '\\Z']

I'd like to get a result:

l = ['\number', '\A', '\b', '\B', '\d', '\D', '\s', '\S', '\w', '\W', '\Z']

Handle it with regex

In [35]: [re.sub(r'\\(?=\\w+)', '',i) for i in l]
Out[35]:
['\\',
 '\\number',
 '\\A',
 '\\b',
 '\\B',
 '\\d',
 '\\D',
 '\\s',
 '\\S',
 '\\w',
 '\\W',
 '\\Z']

It failed to work as I expected.

How to accomplish such a task using regex?

1 Answers1

0

Search By this:

\\\\(?!\\)

and

replace by this:

\\

Regex Demo

Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43