使用python正则表达式排除'';在末尾,但不是在字符串中
我正在尝试使用python正则表达式来识别使用python正则表达式排除'';在末尾,但不是在字符串中,python,html,regex,Python,Html,Regex,我正在尝试使用python正则表达式来识别@提及的内容,例如@user和@user.name 到目前为止,我已经: htmlcontent = re.sub(r'((\@)([\w\.-]+))', r"a href='/users/\3'>\1 /a>", htmlcontent) 当此代码发现一个以结尾的时,不排除它: e、 你好@user.name.你好吗 迄今为止的产出: @user.name/a> 期望输出: @user.name/a>试试这个: re.sub(r'((
@提及的内容,例如@user
和@user.name
到目前为止,我已经:
htmlcontent = re.sub(r'((\@)([\w\.-]+))', r"a href='/users/\3'>\1 /a>", htmlcontent)
当此代码发现一个以
结尾的
时,不排除它:
e、 你好@user.name.
你好吗
迄今为止的产出:
@user.name/a>
期望输出:
@user.name/a>
试试这个:
re.sub(r'((\@)([\w.-]+[\w]+))', r"<a href='/users/\3'>\1</a>", htmlcontent)
re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
这会让RE引擎知道,'.'和'-'可以在中间-但是字符串必须以字符结束。
以您的示例为例:
In [3]: htmlcontent = 'Hi @user.name. How are 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 are you?"
[3]中的:htmlcontent='Hi@user.name。你好吗?”
在[4]:re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
外出[4]:“嗨,你好吗?”
试试这个:
re.sub(r'((\@)([\w.-]+[\w]+))', r"<a href='/users/\3'>\1</a>", htmlcontent)
re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
这会让RE引擎知道,'.'和'-'可以在中间-但是字符串必须以字符结束。
以您的示例为例:
In [3]: htmlcontent = 'Hi @user.name. How are 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 are you?"
[3]中的:htmlcontent='Hi@user.name。你好吗?”
在[4]:re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
外出[4]:“嗨,你好吗?”
试试这个:
re.sub(r'((\@)([\w.-]+[\w]+))', r"<a href='/users/\3'>\1</a>", htmlcontent)
re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
这会让RE引擎知道,'.'和'-'可以在中间-但是字符串必须以字符结束。
以您的示例为例:
In [3]: htmlcontent = 'Hi @user.name. How are 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 are you?"
[3]中的:htmlcontent='Hi@user.name。你好吗?”
在[4]:re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
外出[4]:“嗨,你好吗?”
试试这个:
re.sub(r'((\@)([\w.-]+[\w]+))', r"<a href='/users/\3'>\1</a>", htmlcontent)
re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
这会让RE引擎知道,'.'和'-'可以在中间-但是字符串必须以字符结束。
以您的示例为例:
In [3]: htmlcontent = 'Hi @user.name. How are 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 are you?"
[3]中的:htmlcontent='Hi@user.name。你好吗?”
在[4]:re.sub(r'(\@)([\w.-]+[\w]+)',r',htmlcontent)
外出[4]:“嗨,你好吗?”
您可以在比赛结束时对
进行积极的前瞻,如
([\w\.-]+)(?=\.\s)?
示例
string = "Hi @user.name. How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name.'>user.name. /a> How are you?
string = "Hi @user.name How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name'>user.name /a> How are you?
您可以在比赛结束时对
进行积极的前瞻,如
([\w\.-]+)(?=\.\s)?
示例
string = "Hi @user.name. How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name.'>user.name. /a> How are you?
string = "Hi @user.name How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name'>user.name /a> How are you?
您可以在比赛结束时对
进行积极的前瞻,如
([\w\.-]+)(?=\.\s)?
示例
string = "Hi @user.name. How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name.'>user.name. /a> How are you?
string = "Hi @user.name How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name'>user.name /a> How are you?
您可以在比赛结束时对
进行积极的前瞻,如
([\w\.-]+)(?=\.\s)?
示例
string = "Hi @user.name. How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name.'>user.name. /a> How are you?
string = "Hi @user.name How are you?"
print re.sub(r'@([\w\.-]+)(?=\.\s)?', r"a href='/users/\1'>\1 /a>", string)
#Output
#Hi a href='/users/user.name'>user.name /a> How are you?