html - Using python regex to exclude '.' at the end but not inside a string -


i trying use python regex spot @mentions such @user , @user.name

so far have:

htmlcontent = re.sub(r'((\@)([\w\.-]+))', r"a href='/users/\3'>\1 /a>", htmlcontent) 

when code spots @mention ending in . not exclude it:

e.g. hi @user.name. how you?

output far:

<a href='/users/user.name.'>@user.name. /a>

desired output:

<a href='/users/user.name'>@user.name /a> <-- without . after name

try this:

re.sub(r'((\@)([\w.-]+[\w]+))', r"<a href='/users/\3'>\1</a>", htmlcontent) 

this let re engine know '.' , '-' can in middle - string must end on character. running on example:

in [3]: htmlcontent = 'hi @user.name. how you?' in [4]: re.sub(r'((\@)([\w.-]+[\w]+))', r"<a href='/users/\3'>\1</a>", htmlcontent) out[4]: "hi <a href='/users/user.name'>@user.name</a>. how you?" 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -