Regular Expressions

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

    Regular Expressions


    I am fairly new to php but I think I must be completely missing the
    poiny here (or going mad)

    I would have expected this to match, am I right?

    if (eregi("a89", "a.[0-9]" ))
    {echo "<br>match" ;}
    else {echo "<br>no match";}

    Thanks for any help

    John

  • Alvaro G Vicario

    #2
    Re: Regular Expressions

    *** John wrote/escribió (Fri, 05 Aug 2005 08:35:46 +0100):[color=blue]
    > I would have expected this to match, am I right?
    >
    > if (eregi("a89", "a.[0-9]" ))
    > {echo "<br>match" ;}
    > else {echo "<br>no match";}[/color]

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Description
    int eregi ( string pattern, string string [, array &regs] )

    So your patter is 'a89'. It contains no special chars, so it's a literal.
    And, definitively, it isn't contained in your string 'a.[0-9]'. It should
    *not* match.

    BTW, I'd say most PHP coders use Perl-comaptible regular expressions
    (PCRE):

    Regular Expressions (Perl-Compatible)



    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • Colin McKinnon

      #3
      Re: Regular Expressions

      Alvaro G Vicario wrote:
      [color=blue]
      > *** John wrote/escribió (Fri, 05 Aug 2005 08:35:46 +0100):[color=green]
      >> I would have expected this to match, am I right?
      >>
      >> if (eregi("a89", "a.[0-9]" ))
      >> {echo "<br>match" ;}
      >> else {echo "<br>no match";}[/color]
      >
      > http://www.php.net/eregi
      >
      > Description
      > int eregi ( string pattern, string string [, array &regs] )
      >
      > So your patter is 'a89'. It contains no special chars, so it's a literal.
      > And, definitively, it isn't contained in your string 'a.[0-9]'. It should
      > *not* match.
      >[/color]

      Yup, I think you got the params the wrong way round A. RTFM. (and it works
      when you put them the right way).
      [color=blue]
      > BTW, I'd say most PHP coders use Perl-comaptible regular expressions
      > (PCRE):
      >[/color]

      What makes you say that J? I find it much more sensible to use the same
      syntax that javascript supports (almost).

      C.

      Comment

      • Erwin Moller

        #4
        Re: Regular Expressions

        John wrote:
        [color=blue]
        >
        > I am fairly new to php but I think I must be completely missing the
        > poiny here (or going mad)
        >
        > I would have expected this to match, am I right?
        >
        > if (eregi("a89", "a.[0-9]" ))
        > {echo "<br>match" ;}
        > else {echo "<br>no match";}
        >
        > Thanks for any help
        >
        > John[/color]

        Hi,

        I think you should switch the arguments to eregi().
        First pattern, than the string to unleash the pattern on.
        Check www.php.net for the definition of the function.

        Regards,
        Erwin Moller

        Comment

        • Alvaro G Vicario

          #5
          Re: Regular Expressions

          *** Colin McKinnon wrote/escribió (Fri, 05 Aug 2005 09:51:14 +0100):[color=blue][color=green]
          >> BTW, I'd say most PHP coders use Perl-comaptible regular expressions
          >> (PCRE):[/color]
          >
          > What makes you say that J?[/color]

          No stats, just an subjective impression. Most questions about regex I read
          here seem to use preg_match() or preg_replace().


          --
          -- Álvaro G. Vicario - Burgos, Spain
          -- http://bits.demogracia.com - Mi sitio sobre programación web
          -- Don't e-mail me your questions, post them to the group
          --

          Comment

          • Ewoud Dronkert

            #6
            Re: Regular Expressions

            On Fri, 5 Aug 2005 11:26:56 +0200, Alvaro G Vicario wrote:[color=blue]
            > No stats, just an subjective impression. Most questions about regex I read
            > here seem to use preg_match() or preg_replace().[/color]

            Also, the manual suggests it: "preg_match (), which uses a Perl-compatible
            regular expression syntax, is often a faster alternative to ereg()."
            --

            Vote for 'Default quoting of previous message in replies'

            Comment

            • JDS

              #7
              Re: Regular Expressions

              On Fri, 05 Aug 2005 09:51:14 +0100, Colin McKinnon wrote:
              [color=blue]
              > What makes you say that J? I find it much more sensible to use the same
              > syntax that javascript supports (almost).[/color]

              Well I find it more sensible to use the same syntax Perl supports. So
              there.

              (Just pointing out the arbitrainess of your statement.)

              Actually, though, my subjective assessment comes to the same conclusion as
              Alvaro -- that preg_* is more commonly used than ereg*. If for no other
              reason than the "faster alternative" reason stated on the PHP website.

              --
              JDS | jeffrey@example .invalid
              | http://www.newtnotes.com
              DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

              Comment

              • John

                #8
                Re: Regular Expressions

                On Fri, 05 Aug 2005 08:35:46 +0100, John <asdvfasd@asdfv rfg> wrote:
                [color=blue]
                >
                >I am fairly new to php but I think I must be completely missing the
                >poiny here (or going mad)[/color]

                What can I say apart from DOLT.

                Well for a start, sorry for missing this and thanks for your patience.

                Belive it or not I did RTFM. Many times and looked on various sites to
                get a different view. I can only assume I had a blockage such that
                when it said 'pattern' I read it as the pattern to be tested. Heyho.

                All I have to do now is work out the test I need.

                Thanks again, all comments much appreciated.

                --
                John


                PS Sorry for posting seperately as well. Shan't do that again, after
                this

                Comment

                Working...