Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中从字符串中剥离@tags_Python_Regex - Fatal编程技术网

在python中从字符串中剥离@tags

在python中从字符串中剥离@tags,python,regex,Python,Regex,我试图使用正则表达式从python中的字符串中删除@标记,但是当我尝试这样做时 str = ' you @warui and @madawar ' h = re.search('@\w*',str,re.M|re.I) print h.group() 它只输出第一个@tag @warui 当我尝试使用它时,使用正则表达式从字符串中删除@标记是有效的 或者你想提取它们 @标记定义为@后跟至少一个字母数字字符,这就是为什么@w+比@w*更好。此外,您不需要修改大小写敏感度,因为\w同时匹配小写

我试图使用正则表达式从python中的字符串中删除@标记,但是当我尝试这样做时

str = ' you @warui  and @madawar '
h = re.search('@\w*',str,re.M|re.I)
print h.group()
它只输出第一个@tag

@warui
当我尝试使用它时,使用正则表达式从字符串中删除@标记是有效的

或者你想提取它们

@标记定义为@后跟至少一个字母数字字符,这就是为什么@w+比@w*更好。此外,您不需要修改大小写敏感度,因为\w同时匹配小写和大写字符。

使用正则表达式从字符串中删除@标记

import re
s = ' you @warui  and @madawar '
for h in re.findall('@\w*',s,re.M|re.I):
  print h
或者你想提取它们

@标记定义为@后跟至少一个字母数字字符,这就是为什么@w+比@w*更好。此外,您不需要修改区分大小写,因为\w同时匹配小写和大写字符

import re
s = ' you @warui  and @madawar '
for h in re.findall('@\w*',s,re.M|re.I):
  print h
印刷品:

@瓦瑞

@马达瓦

印刷品:

@瓦瑞

@马达瓦


重新搜索将只匹配模式的一个匹配项。如果要查找更多,请尝试使用re.findall。

re.search将只匹配该模式的一个匹配项。如果您想找到更多,请尝试使用re.findall。

如果您真的想删除标记,您可能还想使用r'@\w+\w'去除标记不必要的尾随空白。@Kimvais-虽然这不是一个坏主意,但可能需要多一点逻辑才能正确构建:hello@world,你好吗?->你好,你好吗?你好,你好吗?如果你真的想删除标记,你可能还想使用r'@\W+\W'删除标记不必要的尾随空格。@Kimvais-虽然这不是一个坏主意,但可能需要更多的逻辑来正确构建:hello@world,你好吗?->你好,你好吗?你好,你好吗?最好去掉前导空格:r'\W@\W+'
import re
s = ' you @warui  and @madawar '
for h in re.findall('@\w*',s,re.M|re.I):
  print h