PHP regular expressions

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default 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
    Albert Guest

  2. Similar Questions and Discussions

    1. RE : RE : Regular expressions
      I think I begin to understand... I begin by fetching the results of the ps -efA command and split it into many variables ($uid, $pid, etc.) ...
    2. [PHP] Q on Regular Expressions
      * Thus wrote jsWalter (jsWalter@torres.ws): have you looked at strtotime()? http://php.net/strtotime The Date formats it can find are defined...
    3. [PHP-DEV] PHP regular expressions
      Hello. In regex/utils.h there is a definition for DUPMAX: #ifdef _POSIX2_RE_DUP_MAX #define DUPMAX _POSIX2_RE_DUP_MAX #else #define DUPMAX 255...
    4. Help with regular expressions.
      Apples and Oranges: I think your format string is wrong, try: {0:d} "Pablo Pecora" <pablo.pecora@itau.com.ar> wrote in message...
    5. help with regular expressions
      Hello, just getting grips with Perl and RE, but need your help. I am trying to open a file, print its contents to a textbox, but extract...
  3. #2

    Default Re: PHP regular expressions

    Albert <albert@arbel-designs.com> wrote:
    > 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?
    So basically you need a regexp tutorial. I don't know any but studing
    [url]http://nl.php.net/manual/en/pcre.pattern.syntax.php[/url] 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

    Daniel Tryba Guest

  4. #3

    Default Re: PHP regular expressions

    [url]http://us2.php.net/manual/en/ref.pcre.php[/url]


    "Albert" <albert@arbel-designs.com> wrote in message
    news:748f323.0310280847.20d8ae91@posting.google.co m...
    > 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

    Brian Guest

  5. #4

    Default Re: PHP regular expressions

    An noise sounding like Daniel Tryba said:
    > Albert <albert@arbel-designs.com> wrote:
    >> 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?
    >
    > So basically you need a regexp tutorial. I don't know any but studing
    > [url]http://nl.php.net/manual/en/pcre.pattern.syntax.php[/url] 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.
    >
    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!

    David Gillen Guest

  6. #5

    Default Re: PHP regular expressions

    In article <slrnbptabt.gh9.Belial@carbon.redbrick.dcu.ie>,
    David Gillen <Belial@RedBrick.DCU.IE> wrote:
    > An noise sounding like Daniel Tryba said:
    > > Albert <albert@arbel-designs.com> wrote:
    > >> 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?
    > >
    > > So basically you need a regexp tutorial. I don't know any but studing
    > > [url]http://nl.php.net/manual/en/pcre.pattern.syntax.php[/url] 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.
    > >
    > 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.
    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 [url]http://php.net/pcre.pattern.syntax[/url] and [url]http://php.net/strlen[/url] for more info.


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

    --
    CC
    CC Zona Guest

  7. #6

    Default Re: PHP regular expressions

    Albert:
    > 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?
    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
    André Nęss Guest

  8. #7

    Default Re: PHP regular expressions

    David Gillen <Belial@redbrick.dcu.ie> wrote:
    >>> 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?
    >>
    >> /a+b+/
    >>
    > 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.
    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

    Daniel Tryba Guest

  9. #8

    Default Re: PHP regular expressions

    [email]albert@arbel-designs.com[/email] (Albert) wrote in message news:<748f323.0310280847.20d8ae91@posting.google.c om>...
    > 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.

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

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

    ---
    "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
    R. Rajesh Jeba Anbiah Guest

  10. #9

    Default Re: PHP regular expressions

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

    THis one is squarely focused on PHP.
    [url]http://www.phpedit.net/products/RegExpEditor/[/url]


    Brian Guest

  11. #10

    Default 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
    Albert Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139