Regex - I only want to match the start tags in regex

后端 未结 5 1521
醉话见心
醉话见心 2021-01-06 23:36

I am making a regex expression in which I only want to match wrong tags like:

*some text here, some other tags may be here as well but no ending \'p\' tag* &

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-07 00:08

    All of the solutions offered so far match the second

    , but that's wrong. What if there are two consecutive

    elements without closing tags? The second one won't be matched because the first match ate its opening tag. You can avoid that problem by using a lookahead as I did here:

    @"(?:[^<]+|<(?!/?p>))*)(?=

    As for the rest of it, I used a "not the initial or not the rest" technique along with an atomic group to guide the regex to a match as efficiently as possible (and, more importantly, to fail as quickly as possible if it's going to).

提交回复
热议问题