Python 正在尝试打印字符串中的前后字符
在python中,我试图在“©符号前后提取4个charters,这段代码提取©之后的字符,有人能帮助打印©之前的字符吗(我不想打印整个字符串,只有几个字符)Python 正在尝试打印字符串中的前后字符,python,Python,在python中,我试图在“©符号前后提取4个charters,这段代码提取©之后的字符,有人能帮助打印©之前的字符吗(我不想打印整个字符串,只有几个字符) 重新导入 html=“这是所有正在进行的测试和尝试,兄弟版权所有©兄弟代码兄弟” 如果html中有“?”: symbol=re.findall(r“(?试着这样做: if "©" in html: pos_c = html.find("©") symbol = html[pos_c-4:pos_c] print sy
重新导入
html=“这是所有正在进行的测试和尝试,兄弟版权所有©兄弟代码兄弟”
如果html中有“?”:
symbol=re.findall(r“(?试着这样做:
if "©" in html:
pos_c = html.find("©")
symbol = html[pos_c-4:pos_c]
print symbol
你快到了
使用搜索获取索引,然后根据需要对字符串进行切片/切分
symbol=re.search(r"(?<=©).+$",html).start()
输出:
ight
Bro
请使用python内置函数split()来解决此问题
html=“这是所有正在进行的测试和尝试,兄弟版权所有©兄弟代码兄弟”
html=html.split(“”)
这里有一个仅限正则表达式的解决方案用于获取©前后的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']
他说,我正试图在“)”符号前后提取4名租船人
您的解决方案没有提供这一点。@Pandsh很高兴我能提供帮助。
ight
Bro
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']