简体   繁体   中英

Find all words after a specific word and replace it with HTML code

I have a log file :

USER INPUT :  “clear”                   
SYSTEM RESPONSE: “Hello! How are you?”        
USER INPUT : “Good thank you”                 
SYSTEM RESPONSE: "Okay"                 
USER INPUT : “clear”                    
SYSTEM RESPONSE: “Hello! How are you?”        
USER INPUT : “I am good, Thank you!”          
SYSTEM RESPONSE: "Great!"                     
USER INPUT : “Good” 

and my current code for them is in python and html :

import re
from pprint import pprint


log_file = """USER INPUT :  "clear"
SYSTEM RESPONSE: Hello! How are you?
USER INPUT : Good thank you
SYSTEM RESPONSE: Okay
USER INPUT : "clear"
SYSTEM RESPONSE: Hello! How are you?
USER INPUT : I am good, Thank you!
SYSTEM RESPONSE: Great!
USER INPUT : Good"""


groups = re.findall(r'USER INPUT.*?clear.*?(?:(?=USER INPUT :\s+\Wclear\W)|(?=\Z))', log_file, flags=re.DOTALL)

html=''
data = []
for d, g in zip(data, groups):
    for line in groups.splitlines():
        html += """<p class = " tooltip" style="color:green;" >"""+  line + '<span class=" tooltiptext">Tooltip text</span> </p>\n'

pprint("""<html>
<style>
.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;

}

.tooltip .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;

    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
</style><body>"""+ html)

My code currently adds the hover feature for all the lines, but I only want it for anything that follows USER INPUT: "....." Any suggestions will be great! Thank you! Please feel free to ask me for any questions or clarifications

Does this do what you want?

for d, g in zip(data, groups):
    for line in groups.splitlines():
        if line.startswith("USER INPUT"):
            html += """<p class = " tooltip" style="color:green;" >"""+  line + '<span class=" tooltiptext">Tooltip text</span> </p>\n'
        else:
            html += """<p style="color:green;" >"""+  line + </p>\n'
import re
from pprint import pprint


log_file = """USER INPUT :  "clear"
SYSTEM RESPONSE: Hello! How are you?
USER INPUT : Good thank you
SYSTEM RESPONSE: Okay
USER INPUT : "clear"
SYSTEM RESPONSE: Hello! How are you?
USER INPUT : I am good, Thank you!
SYSTEM RESPONSE: Great!
USER INPUT : Good"""


groups = re.findall(r'USER INPUT.*?clear.*?(?:(?=USER INPUT :\s+\Wclear\W)|(?=\Z))', log_file, flags=re.DOTALL)

html=''
data = []
                        for d, g in zip(data, groups):
                            for line in g.splitlines():
                                if "USER INPUT" in line :
                                    html += stylep[d] + "USER INPUT" + line[10:] + '<span class=" tooltiptext">Tooltip text</span> </p>\n'
                                else:
                                    html += style[d]+ line + '</p>\n'

pprint("""<html>
<style>
.tooltip {
    position: relative;
    display: inline-block;
    border-bottom: 1px dotted black;

}

.tooltip .tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;

    /* Position the tooltip */
    position: absolute;
    z-index: 1;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
}
</style><body>"""+ html)

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