A regular expression that does not close <

I am looking for a regex for use in php (possibly with preg replacement?) That breaks the text into all unclosed <and ONLY unclosed and all unopened >and ONLY closed ones.

Some examples:

1

<name> aaaaaa bbbbb <  aagfetfe <aaaa/>
to
<name> aaaaaa bbbbb   aagfetfe <aaaa/>

2

<<1111>sbab  < amkka <pippo>
to
<1111>sbab   amkka <pippo>

3

<1111> aaaa <    thehehe  > aaaaaa <ciao>
to
<1111> aaaa <    thehehe  > aaaaaa <ciao>

4

<1111> aaaa   thehehe  > aaaaaa <ciao>
to 
<1111> aaaa   thehehe   aaaaaa <ciao>

5

<1111> aaaa   thehehe  < aaaaaa
to 
<1111> aaaa   thehehe   aaaaaa

I really can't make it too complicated for me.

+5
source share
2 answers
$s = preg_replace("/<([^<>]*)(?=<|$)/", "$1", $s); # remove unclosed '<'
$s = preg_replace("/(^|(?<=>))([^<>]*)>/", "$1", $s); # remove unopened '>'

Do you understand why?

+7
source

< <(?=[^>]*(<|$)) . <, > < . " " - .

> ((^|>)[^<]*)> $1. , > ( ), < >. $1 , >.

+3

All Articles