简体   繁体   中英

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)

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 ©

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

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('©')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM