Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不同html标记对的正则表达式_Html_Regex_Parsing_Shell - Fatal编程技术网

不同html标记对的正则表达式

不同html标记对的正则表达式,html,regex,parsing,shell,Html,Regex,Parsing,Shell,我需要正则表达式匹配每一对…和…来区分聊天对话的各个部分,我收到的是以下格式的字符串: <p CLASS='extmsg'>16:30:24 ~ customer@home.com: hello<br> <p>16:30:14 ~ consultant@company.com: hello to you<br> <p CLASS='extmsg'>16:30:03 ~ sam.i.am@greeneggs.ham: how are yo

我需要正则表达式匹配每一对


来区分聊天对话的各个部分,我收到的是以下格式的字符串:

<p CLASS='extmsg'>16:30:24 ~ customer@home.com: hello<br>
<p>16:30:14 ~ consultant@company.com: hello to you<br>
<p CLASS='extmsg'>16:30:03 ~ sam.i.am@greeneggs.ham: how are you<br>
<p>03/06/2018 16:29:55 ~ bok.kier@ccc.pl: im fine<br> 

16:30:24~customer@home.com:你好
16:30:14~consultant@company.com:你好

16:30:03~sam.i。am@greeneggs.ham:你好吗
2018年6月3日16:29:55~好的。kier@ccc.pl:我很好


我需要它来解析方法。

不要用正则表达式解析HTML,使用合适的XML/HTML解析器

理论: 根据编译理论,不能使用基于的正则表达式解析HTML。由于HTML的层次结构,您需要使用一种语法,并使用类似的工具来操作语法

现实生活™ a中的日常工具: 您可以使用以下选项之一:

(我自己的项目)


检查:


例子:
根据Giles Quenot的回答,正则表达式不适合这种情况。使用适当的解析器是一种更好的方法。如果您确实收到以下格式的邮件:

  • 每行一条消息

  • 每一条消息都以“使用正则表达式进行HTML解析不是……你需要的是首先展示一些研究成果例如:用户想出一种方法将“
    ”放入他们的消息中;这会破坏正则表达式。
    xmllint --html --xpath '//p[@CLASS="extmsg"]/text()' file
    
    var inputString = "" // From wherever you get your data
    var lines = inputString.split("\n")
    for (i = 0; i < lines.length; i++) {
        var line = lines[i]
        if (line.indexOf("<p CLASS='extmsg'>") == 0) {
            console.log("Customer just said: " + line)
        } else {
            console.log("Representative just said: " + line)
        }
    }