How to replace an re match with a transformation of that match?

前端 未结 2 504
长情又很酷
长情又很酷 2021-01-13 09:02

For example, I have a string:

The struct-of-application and struct-of-world

With re.sub

2条回答
  •  佛祖请我去吃肉
    2021-01-13 09:51

    Try this:

    >>> p = re.compile(r"((\w+-)+\w+)")
    >>> p.sub('[\\1](http://\\1)','The struct-of-application and struct-of-world')
    'The [struct-of-application](http://struct-of-application) and [struct-of-world](http://struct-of-world)'
    

提交回复
热议问题