By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,046 Members | 1,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,046 IT Pros & Developers. It's quick & easy.

PHP regular expressions

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Albert <al****@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
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

Jul 17 '05 #2

P: n/a
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
Jul 17 '05 #3

P: n/a
http://us2.php.net/manual/en/ref.pcre.php
"Albert" <al****@arbel-designs.com> wrote in message
news:74*************************@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

Jul 17 '05 #4

P: n/a
An noise sounding like Daniel Tryba said:
Albert <al****@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
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.

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!

Jul 17 '05 #5

P: n/a
In article <sl*******************@carbon.redbrick.dcu.ie>,
David Gillen <Be****@RedBrick.DCU.IE> wrote:
An noise sounding like Daniel Tryba said:
Albert <al****@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
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.

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 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_callback and
then return a boolean as the replace value. A silly workaround, but one
that arguably meets such a requirement...

--
CC
Jul 17 '05 #6

P: n/a
David Gillen <Be****@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

Jul 17 '05 #7

P: n/a
al****@arbel-designs.com (Albert) wrote in message news:<74*************************@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 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
Jul 17 '05 #8

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

THis one is squarely focused on PHP.
http://www.phpedit.net/products/RegExpEditor/
Jul 17 '05 #9

P: n/a
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
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.