
-----------------------------------
Martin
Sat May 28, 2005 2:30 pm

Regular Expression to match alternating characters
-----------------------------------
This is part of my CS assignment, so I'm looking more for hints than an actual solution. I have to write a regular expression to do the following:

All lines in which every other letter is a lowercase a and which contains at least one lowercase a. These a's may be separated by any character (including another a) and the lines may begin with an a or any other character. Thus the lines "a", "aWay", "aha aha" and "Banana" are acceptable, but the lines "b", and "aloha" are not.

-----------------------------------
wtd
Sat May 28, 2005 3:06 pm


-----------------------------------
It's pretty easy.

Match an "a" or "A" one time. 

 /a/i

Then match any character exactly once or zero times.

/a.?/

Now, make this a non-matching group.  Now match that group one or more times.

-----------------------------------
Martin
Sat May 28, 2005 4:22 pm


-----------------------------------
Thanks :)

Yeah...that was pretty easy.

-----------------------------------
wtd
Sat May 28, 2005 5:03 pm


-----------------------------------
For what it's worth, my solution looks like:

/^(?:a.?)+$/i

-----------------------------------
rizzix
Sat May 28, 2005 5:12 pm


-----------------------------------
that won't work... cuz that basically means it has to start with an 'a'

-----------------------------------
wtd
Sat May 28, 2005 5:31 pm


-----------------------------------
The problem description indicated that it does have to start with either an 'a' or 'A'.  The one thing I didn't take into account is the string having to contain at least one lowercase 'a'.  In this event I'd just do something like:

success = str =~ /^(?:a.?)+$/i && str =~ /a/

-----------------------------------
rizzix
Sat May 28, 2005 5:41 pm


-----------------------------------
i think it says it should start with 'a|A' or any other character...

-----------------------------------
wtd
Sat May 28, 2005 5:48 pm


-----------------------------------
How about:

/^(?:.?a)+$/i

-----------------------------------
rizzix
Sat May 28, 2005 5:53 pm


-----------------------------------
that means it has to end with an 'a|A', it will not match "aWay"

-----------------------------------
GlobeTrotter
Sat May 28, 2005 5:55 pm


-----------------------------------
What kind of code is this stuff?  I'm pretty confused.

-----------------------------------
wtd
Sat May 28, 2005 5:59 pm


-----------------------------------
that means it has to end with an 'a|A', it will not match "aWay"

Hmmm...

/^(?:.?a)*.?$/i

-----------------------------------
wtd
Sat May 28, 2005 6:00 pm


-----------------------------------
What kind of code is this stuff?  I'm pretty confused.

We're talking in regular expressions.

[url=http://www.compsci.ca/v2/viewtopic.php?t=6666]What are regular expressions?

There are many more tutorials like that one.

-----------------------------------
rizzix
Sat May 28, 2005 6:07 pm


-----------------------------------
that means it has to end with an 'a|A', it will not match "aWay"

Hmmm...

/^(?:.?a)*.?$/i


that means it will match "b" which is wrong.  :P

-----------------------------------
wtd
Sat May 28, 2005 6:31 pm


-----------------------------------
Ok smartypants, let's see your answer.  :)

-----------------------------------
rizzix
Sat May 28, 2005 6:36 pm


-----------------------------------
/^(?:.?a)+.?$/i

still working on checking for at least one 'a'... :P
