Python 2.7.1我试图使用python正则表达式来提取模式中的单词
我有一些看起来像这样的字符串
someline abc
someother line
name my_user_name is valid
some more lines
Run Code Online (Sandbox Code Playgroud)
我想提取单词"my_user_name".我做的事情
import re
s = #that big string
p = re.compile("name .* is valid", re.flags)
p.match(s) #this gives me <_sre.SRE_Match object at 0x026B6838>
Run Code Online (Sandbox Code Playgroud)
如何立即提取my_user_name?
我正在使用正则表达式检索字符串,并且尝试将其与其他字符串连接起来。
错误:
TypeError: cannot concatenate 'str' and '_sre.SRE_Match' objects
Run Code Online (Sandbox Code Playgroud)
例:
original_string = "ABC 4x DEF"
regex = re.search(r"(\d)X|x", original_string)
new_string = "+" + regex + "00"
print "New string: %s" % new_string
Run Code Online (Sandbox Code Playgroud)
new_string"+400"如果一切正常,应该是。