regex: before and after the match

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • meltedown

    regex: before and after the match

    I want to match a table and also capture the parts before and after the
    table:
    preg_match_all( "|(.*)(<table.* table>)(.*)|",$ text, $parts);
    But of course it doesn't work, presumably because the first .* is
    greedy. I tried adding an ? but that doesn't work either. How do I do
    this ?
  • Garp

    #2
    Re: regex: before and after the match


    "meltedown" <fake@fake.ne t> wrote in message
    news:Ecinc.1495 9330$Id.2475345 @news.easynews. com...[color=blue]
    > I want to match a table and also capture the parts before and after the
    > table:
    > preg_match_all( "|(.*)(<table.* table>)(.*)|",$ text, $parts);
    > But of course it doesn't work, presumably because the first .* is
    > greedy. I tried adding an ? but that doesn't work either. How do I do
    > this ?[/color]

    You might be confused at the results because they contain the table HTML.
    But you were right first time.

    $text="|before< table>stuff goes here</table>after|";
    print htmlspecialchar s($text)."<br>" ;
    preg_match_all( "|(.*?)(<table. *table>)(.*)|", $text, $parts);

    print("Before: ".$parts[1][0]);
    print "<br>Inner: ";
    print htmlspecialchar s($parts[2][0]);
    print "<br>";
    print("After: ".$parts[3][0]);

    Results:
    |before<table>s tuff goes here</table>after|
    Before: |before
    Inner: <table>stuff goes here</table>
    After: after|


    Isn't this what you wanted?

    HTH

    Garp




    Comment

    • Chung Leong

      #3
      Re: regex: before and after the match

      "meltedown" <fake@fake.ne t> wrote in message
      news:Ecinc.1495 9330$Id.2475345 @news.easynews. com...[color=blue]
      > I want to match a table and also capture the parts before and after the
      > table:
      > preg_match_all( "|(.*)(<table.* table>)(.*)|",$ text, $parts);
      > But of course it doesn't work, presumably because the first .* is
      > greedy. I tried adding an ? but that doesn't work either. How do I do
      > this ?[/color]

      Yes, you need to make the first .* non-greedy by appending a ? (probably the
      second .* too). The reason why it doesn't work is because you're not using
      the s modifier, without which . doesn't match newline characters. Make sense
      to use the i modifier as well for case insensitivity. And preg_match()
      should suffice in this case, since you'll at most have one match.


      Comment

      • meltedown

        #4
        Re: regex: before and after the match

        Garp wrote:
        [color=blue]
        > "meltedown" <fake@fake.ne t> wrote in message
        > news:Ecinc.1495 9330$Id.2475345 @news.easynews. com...
        >[color=green]
        >>I want to match a table and also capture the parts before and after the
        >>table:
        >> preg_match_all( "|(.*)(<table.* table>)(.*)|",$ text, $parts);
        >>But of course it doesn't work, presumably because the first .* is
        >>greedy. I tried adding an ? but that doesn't work either. How do I do
        >>this ?[/color]
        >
        >
        > You might be confused at the results because they contain the table HTML.
        > But you were right first time.[/color]
        OK thanks. I did get it to work with preg_match
        I think I was somehow abusing preg_match_all-

        Comment

        Working...