0

I want to put some variables into a regex, but also maintain a regex pattern.

regex = 'set groups {group} routing-instances (?P<routing_instances>[\w\W]+) interface {logical_interface}'.format(
        group=group,
        logical_interface=logical_interface
    )

However, it escapes the escape characters:

ipdb> regex                                                                                                                                             
'set groups GROUP1 routing-instances (?P<routing_instances>[\\w\\W]+) interface a10.555'
sophros
  • 14,672
  • 11
  • 46
  • 75
tread
  • 10,133
  • 17
  • 95
  • 170

1 Answers1

1

Use raw strings:

regex = r'your \regex \here'

Also, it doesn't really matter because your string doesn't actually contain the double slashes, it's the textual representation that contains them.

ForceBru
  • 43,482
  • 10
  • 63
  • 98