Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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 匹配除特定字符串之外的所有内容_Python_Regex_Python 2.7 - Fatal编程技术网

Python 匹配除特定字符串之外的所有内容

Python 匹配除特定字符串之外的所有内容,python,regex,python-2.7,Python,Regex,Python 2.7,我正在使用Python2.7,对正则表达式有一个问题。我的绳子应该是这样的 "SecurityGroup:Pub HDP SG" "SecurityGroup:Group-Name" "SecurityGroup:TestName" 我的正则表达式如下所示 [^S^e^c^r^i^t^y^G^r^o^u^p^:]* 上面的方法似乎有效,但我觉得它不是很有效,而且如果字符串中有单词“group”,那么也会失败 我要查找的是输出应该在冒号后面找到任何内容(:)。我还认为我可以做一些事情,比如

我正在使用Python2.7,对正则表达式有一个问题。我的绳子应该是这样的

"SecurityGroup:Pub HDP SG" 
"SecurityGroup:Group-Name" 
"SecurityGroup:TestName"
我的正则表达式如下所示

[^S^e^c^r^i^t^y^G^r^o^u^p^:]*
上面的方法似乎有效,但我觉得它不是很有效,而且如果字符串中有单词“group”,那么也会失败

我要查找的是输出应该在冒号后面找到任何内容(
)。我还认为我可以做一些事情,比如使用第二组作为我的对手。。。但问题是,如果名称中有空格,那么我将无法获得正确的名称

(SecurityGroup):(\w{1,})
为什么不直接做呢

security_string.split(':')[1]
要抓住冒号后面字符串的第二部分?

可以使用:

pattern=re.compile(r)(?可能是这样的:

([^:"]+[^\s](?="))

如果所有字符串总是以“SecurityGroup:”开头,为什么不从每个字符串中删除前14个字符?请给我们一个输入的代表性示例。如果总是“SecurityGroup”,则不需要正则表达式,只需使用str.find()即可找到该字符串提示,只使用一个负数:
[^SecurityGroup:
regex中的
u
在哪里?regex很少是最具Python风格的解决方案。为什么不简单地使用
r“SecurityGroup:(.*)”
?很好,看起来它也可以完成同样的事情。但是lookbehinds很酷!我想在这种情况下不需要它。
  (?<=  # positive lookbehind. Matches things preceded by the following group
    SecurityGroup:  # pattern you want your matches preceded by
  )  # end positive lookbehind
  (  # start matching group
    .*  # any number of characters
  )  # end matching group
([^:"]+[^\s](?="))