Python正则表达式匹配除tab之外的所有字符



如何匹配除水平制表符之外的任何字符?我要捕获的组是以":"开头、后跟"\t"的字符。

regex = re.compile(r'^p:([^t]*)t')
line = 'p:452c942b93tpersontSimon Sturridge'
if regex.match(line):
    print 'MATCH'

谢谢。

编辑:我想匹配这种格式的字符串"p:"+"以及数字和字母"+'\t'"
并捕获":"后面和"\t"前面的随机数字和字母。我为不够简洁而道歉。

根据您的描述,我想说:

pattern = re.compile(r'^p:(w*)t')
foo = re.match(pattern, line)
if foo:
  print 'MATCH'

测试:

>>> foo.groups()
('452c942b93',)
>>>foo.group(1)
'452c942b93'

最新更新