regexp prob

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

    #1

    regexp prob

    this is driving my crazy:

    $set = '<Error Type="string" Value="System Error" /><ErrorCode
    Type="int" Value="0" />';
    preg_match('%<E rror .*? Value="(.*)"%', $set, $matches);
    echo $matches[1];


    returns: System Error" />

    why on earth does it include the " /> part when my pattern clearly asks
    only to capture what's between the double quotes with "(.*)" ?
  • Simon Stienen

    #2
    Re: regexp prob

    Filip <filip.camerman @mrbookmaker-nv.no.spam.be> wrote:[color=blue]
    > this is driving my crazy:
    >
    > $set = '<Error Type="string" Value="System Error" /><ErrorCode
    > Type="int" Value="0" />';
    > preg_match('%<E rror .*? Value="(.*)"%', $set, $matches);
    > echo $matches[1];
    >
    >
    > returns: System Error" />
    >
    > why on earth does it include the " /> part when my pattern clearly asks
    > only to capture what's between the double quotes with "(.*)" ?[/color]

    Because .* without trailing question mark will consume as many characters
    as possible. In your example that would be:
    <Error Type="string" Value="System Error" /><ErrorCode Type="int" Value="0" />';
    ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^

    --
    Simon Stienen <http://dangerouscat.ne t> <http://slashlife.de>
    »What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done.«
    -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

    Comment

    • Filip

      #3
      Re: regexp prob

      Simon Stienen wrote:[color=blue]
      > Filip <filip.camerman @mrbookmaker-nv.no.spam.be> wrote:
      >[color=green]
      >>this is driving my crazy:
      >>
      >>$set = '<Error Type="string" Value="System Error" /><ErrorCode
      >>Type="int" Value="0" />';
      >>preg_match('% <Error .*? Value="(.*)"%', $set, $matches);
      >>echo $matches[1];
      >>
      >>
      >>returns: System Error" />
      >>
      >>why on earth does it include the " /> part when my pattern clearly asks
      >>only to capture what's between the double quotes with "(.*)" ?[/color]
      >
      >
      > Because .* without trailing question mark will consume as many characters
      > as possible. In your example that would be:
      > <Error Type="string" Value="System Error" /><ErrorCode Type="int" Value="0" />';
      > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
      >[/color]

      ah I was sure that wasn't the problem because it only showed " />, but I
      guess anything starting with < is not shown in the browser. In any case,
      adding the ? made it work, thanx!

      Comment

      • Michael Fesser

        #4
        Re: regexp prob

        .oO(Filip)
        [color=blue]
        >ah I was sure that wasn't the problem because it only showed " />, but I
        >guess anything starting with < is not shown in the browser.[/color]

        While debugging use

        htmlspecialchar s(echo $matches[1])

        instead, if the result contains HTML-code. Or use

        header('Content-type: text/plain');

        somewhere before.
        [color=blue]
        >In any case,
        >adding the ? made it work, thanx![/color]

        If you have more quantifiers in your pattern you could also use the
        modifier U which turns all quantifiers to ungreedy.

        Micha

        Comment

        Working...