Regex to Select HTML Tag (& Contained Text) ONLY When It's Multiline - Stack Overflow

admin2025-05-01  0

I have a very large document that contains HTML tags that are split onto multiple lines. All the formatters I've tried do not format them onto single lines.

The regex I have is:

<p>(.|\n)*?<\/p>

However this is selecting both single line and multiline cases.

Here are some test cases (I've noted expected outcome, currently all four are being selected):

// Should NOT match, as it's all on one line:
<p><b>start -</b> Uses an area level ("Name field" from levels.txt) to define where the player starts in the Act</p>

// SHOULD match, as it's multiline:
<p><b>maxnpcitemlevel -</b> Controls the maximum item level for items sold by the NPC in the Act
                    </p>

// Should NOT match, as it's all on one line:
<p>This file controls global Act functionalities including item levels, monster behaviors, and waypoints</p>

// SHOULD match, as it's multiline:
<p><b>wanderingMonsterRegionTotal -</b> The maximum number of wandering monsters allowed at once
                    </p>

I've created a regex101:

I have a very large document that contains HTML tags that are split onto multiple lines. All the formatters I've tried do not format them onto single lines.

The regex I have is:

<p>(.|\n)*?<\/p>

However this is selecting both single line and multiline cases.

Here are some test cases (I've noted expected outcome, currently all four are being selected):

// Should NOT match, as it's all on one line:
<p><b>start -</b> Uses an area level ("Name field" from levels.txt) to define where the player starts in the Act</p>

// SHOULD match, as it's multiline:
<p><b>maxnpcitemlevel -</b> Controls the maximum item level for items sold by the NPC in the Act
                    </p>

// Should NOT match, as it's all on one line:
<p>This file controls global Act functionalities including item levels, monster behaviors, and waypoints</p>

// SHOULD match, as it's multiline:
<p><b>wanderingMonsterRegionTotal -</b> The maximum number of wandering monsters allowed at once
                    </p>

I've created a regex101: https://regex101.com/r/XLflH4/2

Share Improve this question asked Jan 2 at 19:28 WolfieeifloWWolfieeifloW 6191 gold badge10 silver badges30 bronze badges 1
  • stackoverflow.com/a/1732454/460557 just as a reminder... – Jorge Campos Commented Jan 2 at 19:50
Add a comment  | 

1 Answer 1

Reset to default 1

I have found the following regex that seems to work for what I need:

<p>(?:(?!<\/p>).)*\n(?:(.|\n)*?)<\/p>
转载请注明原文地址:http://anycun.com/QandA/1746100541a91673.html