尝试在字符串之前和之后打印

时间:2017-05-19 06:51:17

标签: python

在python中,我试图在'©'符号前后提取4个包机,此代码在©之后提取字符,任何人都可以帮助在©之前打印字符(我不希望整个字符串得到打印) ,只有几个字符)

import re
    html = "This is all test and try things that's going on bro Copyright© Bro Code Bro"
    if "©" in html:
        symbol=re.findall(r"(?<=©).+$",html,re.M)
        print(symbol[0][0:100])

5 个答案:

答案 0 :(得分:2)

这是一个正则表达式解决方案,用于获取©

之前和之后的4个字符
import re

text = "This is all test and try things that's going on bro Copyright© Bro Code Bro"

print(re.findall(".{4}©.{4}", text))

输出:

['ight© Bro']

答案 1 :(得分:1)

html = "This is all test and try things that's going on bro Copyright© Bro Code Bro"
html = html.split("©")

print(html[0][-4:])
print(html[1][:4])

输出:

ight
 Bro

答案 2 :(得分:0)

尝试这样做:

if "©" in html:
    pos_c = html.find("©")
    symbol = html[pos_c-4:pos_c]
    print symbol

答案 3 :(得分:0)

你快到了!

使用搜索获取索引,然后根据需要对字符串进行切片/切块

symbol=re.search(r"(?<=©).+$",html).start()

上面一行给出了匹配的索引,在本例中为63

使用

html[symbol:symbol+4] for post and html[symbol-4:symbol] for pre.

答案 4 :(得分:0)

请使用python内置函数split()来解决问题。

html = "This is all test and try things that's going on bro Copyright© Bro Code Bro" html = html.split('©')

相关问题