php and regex (why no =~ like perl)?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • el_roachmeister@yahoo.com

    php and regex (why no =~ like perl)?

    Is there a good article that explains why php does not support =~ like
    perl for regex? I am confused by all the different ways one can do
    regex in php but it would seem supporting =~ would eliminate alot of
    redundancy. What am i missing here?

  • Mladen Gogala

    #2
    Re: php and regex (why no =~ like perl)?

    On Sun, 03 Jul 2005 21:08:30 -0700, el_roachmeister wrote:
    [color=blue]
    > Is there a good article that explains why php does not support =~ like
    > perl for regex? I am confused by all the different ways one can do
    > regex in php but it would seem supporting =~ would eliminate alot of
    > redundancy. What am i missing here?[/color]

    PHP is not Perl. PHP5 has completely different object model, you have the
    same kind of syntax for numeric and hash arrays and you have several types
    of regex functions which can be categorized into preg functions and the
    useless ones. Every other language except Perl (Python, Java, PL/SQL, PHP)
    uses functions for regular expressions. Of course, if you've ever read Lex
    and Yacc book, you know that =~ is also a function, only disguised as an
    operator. Functional syntax is not much harder then the Perl one.

    --


    Comment

    • el_roachmeister@yahoo.com

      #3
      Re: php and regex (why no =~ like perl)?

      The problem is the functional syntax i.e preg_match takes up alot more
      space than a simple =~ . I write web-based software and my code is full
      of regexs so the =~ saves a lot of space.

      Also there seems to be several functions to do regex in php. I am not
      sure which will even be supported in future releases?

      Comment

      • Daniel Tryba

        #4
        Re: php and regex (why no =~ like perl)?

        el_roachmeister @yahoo.com wrote:[color=blue]
        > The problem is the functional syntax i.e preg_match takes up alot more
        > space than a simple =~ . I write web-based software and my code is full
        > of regexs so the =~ saves a lot of space.[/color]

        If you really value typing as less as possible you should create your
        own precompiler (or get a decent editor that does this stuff for you).
        [color=blue]
        > Also there seems to be several functions to do regex in php. I am not
        > sure which will even be supported in future releases?[/color]

        Don't rely on PHP, there will be a newer and better language in the near
        future.

        Comment

        • Andy Hassall

          #5
          Re: php and regex (why no =~ like perl)?

          On 4 Jul 2005 09:33:56 -0700, el_roachmeister @yahoo.com wrote:
          [color=blue]
          >Also there seems to be several functions to do regex in php. I am not
          >sure which will even be supported in future releases?[/color]

          There's two main sorts. The POSIX-compatible ones, e.g. ereg. These come with
          a warning to use the other sort, the Perl-compatible ones (PCRE), preg_replace
          etc. What makes you think either will be removed in a future release anyway?

          (BTW, use the PCRE ones).

          --
          Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
          <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

          Comment

          • Mladen Gogala

            #6
            Re: php and regex (why no =~ like perl)?

            On Mon, 04 Jul 2005 09:33:56 -0700, el_roachmeister wrote:
            [color=blue]
            > The problem is the functional syntax i.e preg_match takes up alot more
            > space than a simple =~ . I write web-based software and my code is full
            > of regexs so the =~ saves a lot of space.[/color]

            Yes, the space argument stands. Most of the modern editors, however,
            are not limited by the line size of 80 characters, so you can create
            nice looking programs that will take a little more space.
            On the other hand, Perl grammar is very complex and full of idioms
            which are unintelligible to the non-consecrated humans. One
            such example is this:

            $a ||= 'Value';

            Translated into PHP, the above expression would be written like this:

            if (!defined($a)) { $a='Value'; }

            I agree that Perl version is much shorter but is also much less
            readable and harder to master. It means that it takes much longer
            to learn the language. Also, variables like $_,@_,$! and $~ are much
            harder to learn. They're also crucial and you cannot use the language
            if you don't know what this means:

            while (<>) {
            chomp;
            print "Line:$_\n" if /^\d+\s{2,}/;
            }

            All of that makes Perl much harder to learn then PHP. The philosophy of
            PHP is similar to the philosophy of the C language: let's make the
            language as simple as possible so that people can learn it quickly.
            That is why PHP became so popular. The object model of PHP is much
            more natural and much easier then the Perl equivalent. Have you ever
            tried @ISA array?
            [color=blue]
            >
            > Also there seems to be several functions to do regex in php. I am not
            > sure which will even be supported in future releases?[/color]

            PCRE library is supported by almost anything. I have no reason to believe
            that they will go away in near future.

            --


            Comment

            • Chung Leong

              #7
              Re: php and regex (why no =~ like perl)?

              I don't see how the object model prevents a shorter regexp syntax from
              being implemented. Regexp could simply be treated as a different type.
              You would something like:

              $re = /\d+/;
              if("12345" == $re) {
              ...
              }

              If they ever decide to integrate regexp in such a way I certainly would
              not object. I wouldn't have a problem with a more compact way of
              initializing arrays either. Heck, while we're at it, I would like named
              arguments too.

              Comment

              • Cristian Gutierrez

                #8
                Re: php and regex (why no =~ like perl)?

                El lunes 4, el roachmeister dijo:[color=blue]
                > Is there a good article that explains why php does not support =~ like
                > perl for regex? I am confused by all the different ways one can do
                > regex in php but it would seem supporting =~ would eliminate alot of
                > redundancy. What am i missing here?[/color]

                Have a look at <http://tnx.nl/php#args>, it may be of help.

                Cheers && good luck,

                --
                Cristian Gutierrez /* crgutier@dcc.uc hile.cl */
                I didn't write this: A very complex macro did.

                Comment

                Working...