0

I first wanted to only match the first instance, but soon realized that is not possible. The tool I'm using only uses RegEx so I have no options as well.

Basically I got a text with HTML tags in it and I want to match the first paragraph's tags without the following tags. For example out of this:

<p>erkfoijwdocndoufhwroguh</p><p>pijgoijkuohuhogiougwtg</p><p>pijgoijkuohuhogiougwtg</p><p>pijgoijkuohuhogiougwtg</p>

I want to match the first <p></p> and nothing else. So I figured I could exclude the tags that have a tag right next to them using negative lookahead. As in:

(?!>)(<|<\/)p>

But for some reason this still matches every <p> and </p> tag instead of leaving out those that have another tag before them. Any suggestions?

Edit to add: I only need to match the tags, not the text inside the tags. And lookbehind doesn't work with the tool I'm using. It seems that everything that works here, works also in my tool.

Second edit: I solved my problem, but I'm leaving the question open since the solution wasn't an answer and this seems like an interesting question and I might bump into similiar problem in the future. Basically if someone figures out how I can refer to <p> that doesn't have a > before it but also include the first </p>, I'd like to hear it.

4

1 回答 1

0

I'm not sure I understood what you are trying to achieve, would this:

^<p>.*?<\/p>

Demo here: https://regex101.com/r/ZXgMPV/1

于 2020-09-21T11:50:58.190 回答