[英]Trying to print after and before character from a string
In python, I'm trying to extract 4 charterers before and after '©' symbol,this code extracts the characters after ©,can anyone help printing the characters before © (I don't want the entire string to get print,only few characters) 在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])
Here's a regex only solution to get the 4 characters before and after the © 这是一个正则表达式解决方案,用于获取©之前和之后的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))
Output: 输出:
['ight© Bro']
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])
Output : 输出:
ight
Bro
Try doing it this way : 尝试这样做:
if "©" in html:
pos_c = html.find("©")
symbol = html[pos_c-4:pos_c]
print symbol
You are almost there! 你快到了!
Use search to get index and then slice/dice the string as you like 使用搜索获取索引,然后根据需要对字符串进行切片/切块
symbol=re.search(r"(?<=©).+$",html).start()
The above line give you the index of the match , in this case 63 上面的行给出了匹配的索引,在本例中为63
Use 采用
html[symbol:symbol+4] for post and html[symbol-4:symbol] for pre.
请使用python内置函数split()来解决问题。
html = "This is all test and try things that's going on bro Copyright© Bro Code Bro" html = html.split('©')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.