简体   繁体   中英

Perform regex on a regex substitution?

Sorry if I used any wrong words in the title, however this has been an issue that has been bugging me.

I have some code to automatically link urls that are inside text.

r = re.compile(r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^%s\s]|/)))')
url_parsed_comment = r.sub(r'<a tabindex=-1 target="blank" href="\1">\1</a>', comment_text)

As you can see, I take the found URL and pass it (\\1) into the href and inside the <a> tag.

I would like to truncate the text inside the <a> tag. The equivalent of what I want would look like this in python:

link = '<a href="' + url + '">' + url[:10] + '...</a>'

How do I accomplish this with my substituted regex variable? How do I limit the characters?

You can use a lambda in re.sub , example:

>>> re.sub(r'([0-9]*)', lambda m: m.group(1)[:2], '---123456789---')
'---12---'

then, you may refer to \\1 by m.group(1) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM