trouble with preg_match

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

    #1

    trouble with preg_match

    Hi all,

    I'm pretty new to PHP. So far I know regular expression only from Perl

    I've written a perl script which I now want to port to PHP. It works in
    PErl, but gives my a headache in PHP.

    I have a line like this:
    "329:47 InitGame:
    \.Admin\xxx\.We bsite\www.yyy.de\g_gametype\hq\gamename\Call of
    Duty\mapname\mp _ship\protocol\ ..."
    I want to get the time at the beginning of the line. Further I need the text
    between "gametype\" and "\gamename" ("hq" in this case) and the text
    between "mapname\" and "\protocol" ("mp_ship").

    The reg ex in Perl looks like this:
    if ($line =~
    m/(\d+:\d+)[\s]*InitGame:.*?_g ametype\\(.*?)\ \.*?mapname\\(. *?)\\/) {...

    It took me quite long time, to find this ugly construct in PHP:
    $match_initGame = '(\d+:\d+)\s*In itGame:.*?_game type' . preg_quote("\\" )
    .. '(.*?)' . preg_quote("\\g amename") . '.*?' . preg_quote("\\m apname\\") .
    '(.*?)' . preg_quote ("\\");
    if (preg_match("/$match_initGame/", $line, $matches ) ) {...

    Is there is any better way to solve my problem?

    Thanks a lot, Thorsten


  • Pedro Graca

    #2
    Re: trouble with preg_match

    Thorsten Walenzyk wrote:[color=blue]
    > I have a line like this:
    > "329:47 InitGame:
    > \.Admin\xxx\.We bsite\www.yyy.de\g_gametype\hq\gamename\Call of
    > Duty\mapname\mp _ship\protocol\ ..."
    > I want to get the time at the beginning of the line. Further I need the text
    > between "gametype\" and "\gamename" ("hq" in this case) and the text
    > between "mapname\" and "\protocol" ("mp_ship").[/color]


    <?php

    $text = '329:47 InitGame:'
    . '\.Admin\xxx\.W ebsite\www.yyy.de\g_gametype\hq\gamename\Call of '
    . 'Duty\mapname\m p_ship\protocol \...';

    $rx = '/
    (\d+:\d+) # get the time
    .* # ignore any number of anything
    gametype # up to "gametype"
    \\\\ # followed by a backslash (*)
    (.*) # get the "gametype" |
    \\\\ # ignore backslash (*)
    gamename # and "gamename" |
    .* # ignore any number of anything |
    mapname\\\\ # up to "mapname\" (*)
    (.*) # get the "mapname" |
    \\\\protocol # followed by "\protocol" (*)
    /x'; # with extended syntax |
    // (*) <----------------------------------------'
    // a single \ between single quotes (or double quotes) is the
    // escape character. As you want a real backslash you have to
    // double it.
    // But that will get passed as a escape character to the preg_match()
    // function! To pass a real backslash to the function, add another
    // pair of backslashes and you're all set.

    if (!preg_match($r x, $text, $matches)) {
    exit("Not found!\n");
    }

    unset($matches[0]);
    print_r($matche s);

    ?>

    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!

    Comment

    • Thorsten Walenzyk

      #3
      Re: trouble with preg_match

      Thanks Pedro.

      It is somehow strange to use 4 backslashes, but it works.

      Thanks, Thorsten


      "Pedro Graca" <hexkid@dodgeit .com> schrieb im Newsbeitrag
      news:slrncqnebf .vuv.hexkid@ID-203069.user.uni-berlin.de...[color=blue]
      > Thorsten Walenzyk wrote:[color=green]
      > > I have a line like this:
      > > "329:47 InitGame:
      > > \.Admin\xxx\.We bsite\www.yyy.de\g_gametype\hq\gamename\Call of
      > > Duty\mapname\mp _ship\protocol\ ..."
      > > I want to get the time at the beginning of the line. Further I need the[/color][/color]
      text[color=blue][color=green]
      > > between "gametype\" and "\gamename" ("hq" in this case) and the text
      > > between "mapname\" and "\protocol" ("mp_ship").[/color]
      >
      >
      > <?php
      >
      > $text = '329:47 InitGame:'
      > . '\.Admin\xxx\.W ebsite\www.yyy.de\g_gametype\hq\gamename\Call of '
      > . 'Duty\mapname\m p_ship\protocol \...';
      >
      > $rx = '/
      > (\d+:\d+) # get the time
      > .* # ignore any number of anything
      > gametype # up to "gametype"
      > \\\\ # followed by a backslash (*)
      > (.*) # get the "gametype" |
      > \\\\ # ignore backslash (*)
      > gamename # and "gamename" |
      > .* # ignore any number of anything |
      > mapname\\\\ # up to "mapname\" (*)
      > (.*) # get the "mapname" |
      > \\\\protocol # followed by "\protocol" (*)
      > /x'; # with extended syntax |
      > // (*) <----------------------------------------'
      > // a single \ between single quotes (or double quotes) is the
      > // escape character. As you want a real backslash you have to
      > // double it.
      > // But that will get passed as a escape character to the preg_match()
      > // function! To pass a real backslash to the function, add another
      > // pair of backslashes and you're all set.
      >
      > if (!preg_match($r x, $text, $matches)) {
      > exit("Not found!\n");
      > }
      >
      > unset($matches[0]);
      > print_r($matche s);
      >
      > ?>
      >
      > --
      > Mail to my "From:" address is readable by all at http://www.dodgeit.com/
      > == ** ## !! ------------------------------------------------ !! ## ** ==
      > TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
      > may bypass my spam filter. If it does, I may reply from another address![/color]


      Comment

      • R. Rajesh Jeba Anbiah

        #4
        Re: trouble with preg_match

        "Thorsten Walenzyk" <thorsten.walen zyk@gmx.de> wrote in message news:<coiqui$eo 0$05$1@news.t-online.com>...[color=blue]
        > Thanks Pedro.
        >
        > It is somehow strange to use 4 backslashes, but it works.[/color]

        You may want to know the difference between single quote and double
        quote <http://in2.php.net/types.string>

        --
        <?php echo 'Just another PHP saint'; ?>
        Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

        Comment

        Working...