[英]How to use regex to find the middle of a string
我正在尝试从Blogger的响应中获得某些结果。 我想获得我的博客名称。 使用Regex我该如何处理? 我已经尝试使用Google搜索我的问题,但不幸的是,没有任何答案对我有帮助。
所以我的回应看起来像这样:
\\x22http://emyblog.blogspot.com/
因此,它始终以\\\\x22http://
开头,以.blogspot.com/
结尾
我试过以下内容:
regEx = re.findall(b"""\x22http://(.*)\.blogspot\.com""", r)
但不幸的是,它返回了一个空列表。 关于如何解决这个问题有什么想法吗?
谢谢,
请使用原始字符串,否则\\\\x22
被解释为字符"
而不是文字字符串。不确定re.findall
方法是否是好的方法, re.search
应该足够。
假设您的字节字符串是:
>>> r = rb'\\x22http://emyblog.blogspot.com/'
使用字节字符串:
>>> res = re.search(rb'\\x22http://(.*)\.blogspot\.com/', r)
>>> res.group(1)
b'emyblog'
使用普通字符串:
>>> res = re.search(r'\\\\x22http://(.*)\.blogspot\.com/', r.decode('utf-8'))
>>> res.group(1)
'emyblog'
使用r''
(将字符串作为原始字符串文字 )而不是b''
import re
pattern = re.compile(r'\x22http://(.*)\.blogspot\.com')
match = pattern.match('\x22http://emyblog.blogspot.com/')
match.group(1)
# 'emyblog'
这似乎正在工作!
import re
text = "\x22http://emyblog.blogspot.com/"
regex = re.compile('\x22http://(.*)\.blogspot\.com')
print regex.findall(text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.