PHP regular expressions

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

    #1

    PHP regular expressions

    Hello,

    I am writing an elearning software with php. I need a script to
    recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.
    Could anyone tell me how can I do this using regular expressions?

    Regards,
    Albert Achtenberg
  • Daniel Tryba

    #2
    Re: PHP regular expressions

    Albert <albert@arbel-designs.com> wrote:[color=blue]
    > I am writing an elearning software with php. I need a script to
    > recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.
    > Could anyone tell me how can I do this using regular expressions?[/color]

    So basically you need a regexp tutorial. I don't know any but studing
    http://nl.php.net/manual/en/pcre.pattern.syntax.php might help.

    But what you want is trivial:

    /a+b+/

    a+ means: 'a' one or more times.
    a+b+ means: 'a' one or more times followed by 'b' one or more times.

    --

    Daniel Tryba

    Comment

    • André Næss

      #3
      Re: PHP regular expressions

      Albert:
      [color=blue]
      > I am writing an elearning software with php. I need a script to
      > recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.
      > Could anyone tell me how can I do this using regular expressions?[/color]

      In general you can't, regular expressions are not powerful enough to deal
      with expression of the type A^nB^n. But there are of course ways to do what
      you want. If you describe the actual problem in more detail we might be
      able to help.

      André Næss

      Comment

      • Brian

        #4
        Re: PHP regular expressions




        "Albert" <albert@arbel-designs.com> wrote in message
        news:748f323.03 10280847.20d8ae 91@posting.goog le.com...[color=blue]
        > Hello,
        >
        > I am writing an elearning software with php. I need a script to
        > recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.
        > Could anyone tell me how can I do this using regular expressions?
        >
        > Regards,
        > Albert Achtenberg[/color]


        Comment

        • David Gillen

          #5
          Re: PHP regular expressions

          An noise sounding like Daniel Tryba said:[color=blue]
          > Albert <albert@arbel-designs.com> wrote:[color=green]
          >> I am writing an elearning software with php. I need a script to
          >> recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.
          >> Could anyone tell me how can I do this using regular expressions?[/color]
          >
          > So basically you need a regexp tutorial. I don't know any but studing
          > http://nl.php.net/manual/en/pcre.pattern.syntax.php might help.
          >
          > But what you want is trivial:
          >
          > /a+b+/
          >
          > a+ means: 'a' one or more times.
          > a+b+ means: 'a' one or more times followed by 'b' one or more times.
          >[/color]
          His problem was trivial, unfortunately you weren't able to see what he
          required. The solution isn't quite as trivial.
          Your solution would allow something along the lines of abbbb. What was asked
          for was the same number of a's and b's.

          db
          --

          I'm a dead man, and buggered to boot!

          Comment

          • CC Zona

            #6
            Re: PHP regular expressions

            In article <slrnbptabt.gh9 .Belial@carbon. redbrick.dcu.ie >,
            David Gillen <Belial@RedBric k.DCU.IE> wrote:
            [color=blue]
            > An noise sounding like Daniel Tryba said:[color=green]
            > > Albert <albert@arbel-designs.com> wrote:[color=darkred]
            > >> I am writing an elearning software with php. I need a script to
            > >> recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.
            > >> Could anyone tell me how can I do this using regular expressions?[/color]
            > >
            > > So basically you need a regexp tutorial. I don't know any but studing
            > > http://nl.php.net/manual/en/pcre.pattern.syntax.php might help.
            > >
            > > But what you want is trivial:
            > >
            > > /a+b+/
            > >
            > > a+ means: 'a' one or more times.
            > > a+b+ means: 'a' one or more times followed by 'b' one or more times.
            > >[/color]
            > His problem was trivial, unfortunately you weren't able to see what he
            > required. The solution isn't quite as trivial.
            > Your solution would allow something along the lines of abbbb. What was asked
            > for was the same number of a's and b's.[/color]

            Yes, he needs a *script* to accomplish that. It doesn't sound like there's
            any requirement for the entire solution to be contained within the
            regex[*]. A capturing regex takes care of the script's first step. Then
            the script can easily be finished with a strlen operation.

            Daniel, see http://php.net/pcre.pattern.syntax and http://php.net/strlen for more info.


            * Though if there were such an arbitrary requirement, one could do the
            length check within the callback function for preg_replace_ca llback and
            then return a boolean as the replace value. A silly workaround, but one
            that arguably meets such a requirement...

            --
            CC

            Comment

            • Daniel Tryba

              #7
              Re: PHP regular expressions

              David Gillen <Belial@redbric k.dcu.ie> wrote:[color=blue][color=green][color=darkred]
              >>> I am writing an elearning software with php. I need a script to
              >>> recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.
              >>> Could anyone tell me how can I do this using regular expressions?[/color]
              >>
              >> /a+b+/
              >>[/color]
              > His problem was trivial, unfortunately you weren't able to see what he
              > required. The solution isn't quite as trivial.
              > Your solution would allow something along the lines of abbbb. What was asked
              > for was the same number of a's and b's.[/color]

              Lets call it selective dyslecia. Scanning a line with aaaabbbb in it, to
              me there appear to be more b's than a's.

              But I should have read the question better..

              --

              Daniel Tryba

              Comment

              • R. Rajesh Jeba Anbiah

                #8
                Re: PHP regular expressions

                albert@arbel-designs.com (Albert) wrote in message news:<748f323.0 310280847.20d8a e91@posting.goo gle.com>...[color=blue]
                > Hello,
                >
                > I am writing an elearning software with php. I need a script to
                > recognize any inut of the type ab,aabb,aaabbb, aaaabbbb..... And so on.[/color]


                $str = "aabb";
                if (preg_match("/(a+)(b+)/", $str, $matches) &&
                (strlen($matche s[1])==strlen($matc hes[2])))
                echo 'match!';

                ***Try this http://weitz.de/regex-coach

                ---
                "Our songs have meaning for everyone. I don't know of anyone who
                isn't either in love, just out of love, or else wants to be in
                love."--- Graham Russell, Air Supply
                Email: rrjanbiah-at-Y!com

                Comment

                • Brian

                  #9
                  Re: PHP regular expressions

                  > ***Try this http://weitz.de/regex-coach

                  THis one is squarely focused on PHP.



                  Comment

                  • Albert

                    #10
                    Re: PHP regular expressions

                    Hi,

                    Thank you for your replies. In fact I was curious to see the replies
                    to my post as what I described is simply impossible. The explanation
                    is that reular expressions can only describe regular languages,
                    whereas {a^nb^n | n=0,1,...} is not a regular language (As one of the
                    replies stated).

                    I'm very sorry if someone was offened by this.

                    Thank you all,
                    Albert Achtenberg

                    Comment

                    Working...