Regular Expression

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

    #1

    Regular Expression

    Someone could tell me how can I get the same result substituting
    ereg with preg_match and ereg_replace with preg_replace.

    $result = ereg("<\[foreach\]>(.+)<\[\/foreach\]>",$this->buffer,$token) ;

    $this->buffer =
    ereg_replace("< \[foreach\]>.+<\[\/foreach\]>","<[foreach$i]>",$this->buffer)
    ;

    Thanks.


  • Zac Hester

    #2
    Re: Regular Expression

    "LuKrOz" <lukroz@virgili o.it> wrote in message
    news:vHOVa.4720 5$Za4.1505374@n ews2.tin.it...[color=blue]
    > Someone could tell me how can I get the same result substituting
    > ereg with preg_match and ereg_replace with preg_replace.
    >
    > $result = ereg("<\[foreach\]>(.+)<\[\/foreach\]>",$this->buffer,$token) ;
    >
    > $this->buffer =
    >[/color]
    ereg_replace("< \[foreach\]>.+<\[\/foreach\]>","<[foreach$i]>",$this->buffer)[color=blue]
    > ;
    >
    > Thanks.
    >[/color]

    It doesn't look like you're using anything specific to Posix Extended
    regular expressions, so you don't have to do much to convert them to Perl
    regular expressions. In Perl regexps, you need some kind of expression
    delimiter (and Posix leaves it up to you if you want one):

    Posix: "<\[foreach\]>(.+)<\[\/foreach\]>"
    Perl: "/<\[foreach\]>(.+)<\[\/foreach\]>/"

    It might even work to leave out the expression delimiter altogether, but I
    haven't tested it with the preg_*() functions. Have you tried your Posix
    pattern in one of the Perl functions?

    When you capture, you can just use a simple backreference in the
    preg_replace() function:

    newstring = preg_replace('/Hello W(.+)\./', 'Goodbye W$1\.', $oldstring);

    Old --> New
    'Hello World.' --> 'Goodbye World.'
    'Hello Wind.' --> 'Goodbye Wind.'
    'Hello Sir.' --> 'Hello Sir.'

    Translating into Perl regular expressions becomes tough if you start using
    Posix character classes, pattern capturing, and look assertions. When
    you're just doing basic string matches, the same expressions will usually
    work in both.

    This may be useful:


    HTH,
    Zac


    Comment

    Working...