1

I'm trying to get regex to match all text between < >, including line breaks and carriage returns.

the farthest I've come is this: (<.*>)|(<.*\n)

The Regex101 string I'm testing with is here: https://regex101.com/r/wuFeG9/1

As you can see, it matches all cases of text between < > except when there is a carriage return/line break in the middle - the red highlights for group 2 show where it's breaking.

Where I'm falling short is when there is a line break/carriage return within the < > brackets. I can't get \s to match it, even though the text in notepad++ shows that there are carriage returns there.

Any help would be greatly appreciated!!
Thanks in advance.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125

2 Answers2

3

You could use

<[^>]*>

Here [^>] means "any characters except >". This is a character class.


Note that if you want to parse HTML, rather than just blindly search-and-replace, regex is not a good method. Also regex cannot cleanly handle broken HTML. See How do you parse and process HTML/XML in PHP? for proper way to do so.

Graham
  • 7,431
  • 18
  • 59
  • 84
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
-1

This should work

REGEXP:

((?:<.*>)|(?<=>)(?:\<.*\n?.*\>))

PHP CODE:

$re = '/((?:<.*>)|(?<=>)(?:\<.*\n?.*\>))/m';
$str = '<p><font face="Helv" size="2"><font face="Helv" size="2">- $25mm to $50mm near term GLRV opportunity. If approved, they would invest in our commingled
fund.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">- This was another critical step in Investcorp&#39;s investment process. In a
positive sign, they&#39;ve decided to concurrently initiate operational due diligence on us and the GLRV fund/strategy.</font></font><br><font face="Helv"
size="2"><font face="Helv" size="2">- Overall an excellent, interactive meeting mainly focused on risk modeling and measurement, and how we use our 
models/tools throughout the investment process to help source and implement trades; actively allocate risk across sub-strategies; as well as adjust the overall risk
taking in the strategy</font></font></p>

MEETING DETAILS:
None Available

NOTES:
<p><font face="Helv" size="2"><font face="Helv" size="2">- Investcorp is a NY based $10 billion global alternative investments manager with roots and a large
presence in the middle east.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">- They are running ~$2 billion in traditional FoHFs AUM (in
addition to another $2 billion in other custom HF mandates) after closing on their SSARIS Advisors (another FoHFs) acquisition earlier this
year.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">- The consolidation of their portfolio continues following the SSARIS acquisition
and he team is finally ready to allocate to new managers. FI Arb/RV is a space they are interested in again.</font></font><br><font face="Helv" size="2"><font
face="Helv" size="2">- We have the advantage of being &quot;first in the door&quot; as they assessed the space, since we&#39;ve been talking to them about LRV
since early 2014.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches);

INPUT:

<p><font face="Helv" size="2"><font face="Helv" size="2">- $25mm to $50mm near term GLRV opportunity. If approved, they would invest in our commingled
fund.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">- This was another critical step in Investcorp&#39;s investment process. In a
positive sign, they&#39;ve decided to concurrently initiate operational due diligence on us and the GLRV fund/strategy.</font></font><br><font face="Helv"
size="2"><font face="Helv" size="2">- Overall an excellent, interactive meeting mainly focused on risk modeling and measurement, and how we use our 
models/tools throughout the investment process to help source and implement trades; actively allocate risk across sub-strategies; as well as adjust the overall risk
taking in the strategy</font></font></p>

MEETING DETAILS:
None Available

NOTES:
<p><font face="Helv" size="2"><font face="Helv" size="2">- Investcorp is a NY based $10 billion global alternative investments manager with roots and a large
presence in the middle east.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">- They are running ~$2 billion in traditional FoHFs AUM (in
addition to another $2 billion in other custom HF mandates) after closing on their SSARIS Advisors (another FoHFs) acquisition earlier this
year.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">- The consolidation of their portfolio continues following the SSARIS acquisition
and he team is finally ready to allocate to new managers. FI Arb/RV is a space they are interested in again.</font></font><br><font face="Helv" size="2"><font
face="Helv" size="2">- We have the advantage of being &quot;first in the door&quot; as they assessed the space, since we&#39;ve been talking to them about LRV
since early 2014.</font></font><br><font face="Helv" size="2"><font face="Helv" size="2">

OUTPUT:

Array
(
    [0] => Array
        (
            [0] => <p><font face="Helv" size="2"><font face="Helv" size="2">
            [1] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
            [2] => </font></font><br>
            [3] => <font face="Helv"
size="2"><font face="Helv" size="2">
            [4] => </font></font></p>
            [5] => <p><font face="Helv" size="2"><font face="Helv" size="2">
            [6] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
            [7] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
            [8] => </font></font><br><font face="Helv" size="2">
            [9] => <font
face="Helv" size="2">
            [10] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
        )

    [1] => Array
        (
            [0] => <p><font face="Helv" size="2"><font face="Helv" size="2">
            [1] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
            [2] => </font></font><br>
            [3] => <font face="Helv"
size="2"><font face="Helv" size="2">
            [4] => </font></font></p>
            [5] => <p><font face="Helv" size="2"><font face="Helv" size="2">
            [6] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
            [7] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
            [8] => </font></font><br><font face="Helv" size="2">
            [9] => <font
face="Helv" size="2">
            [10] => </font></font><br><font face="Helv" size="2"><font face="Helv" size="2">
        )

)

See: https://regex101.com/r/wuFeG9/2

Test PHP CODE: http://ideone.com/2B5n3o

Bohemian
  • 412,405
  • 93
  • 575
  • 722