How to generate a regular expression that does not contain a specific line at the beginning or at the end?

I’m stuck in creating a regular expression that matches any line:
1- contains “<p> \ S </p>” in the middle of the text
2- does not contain <p> \ S </p> at the beginning or end of the text

thanks for the help

+3
source share
1 answer

From the comments you can see that you want to match one space between <p>and </p>. If so, you should use \s, not \s. \smatches any single space character, where \smatches any character without spaces.

You can try:

(?!^<p>\s<\/p>)(?!.*<p>\s<\/p>$)(?=.*<p>\s<\/p>)

(?!^<p>\s<\/p>) , <p>\s<\/p>.

(?!.*<p>\s<\/p>$) , <p>\s<\/p>.

(?=.*<p>\s<\/p>) , <p>\s<\/p> -.

PHP, :

$x="abc <p> </p> def";
$R="#(?!^<p>\s<\/p>)(?!.*<p>\s<\/p>$)(?=.*<p>\s<\/p>)#";                        
$result=preg_match($R,$x); 
var_dump($result); // prints 1.
+2

Source: https://habr.com/ru/post/1783359/


All Articles