Computer Science Canada Regular Expression to match alternating characters |
Author: | Martin [ Sat May 28, 2005 2:30 pm ] |
Post subject: | 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. |
Author: | wtd [ Sat May 28, 2005 3:06 pm ] | ||||
Post subject: | |||||
It's pretty easy. Match an "a" or "A" one time.
Then match any character exactly once or zero times.
Now, make this a non-matching group. Now match that group one or more times. |
Author: | Martin [ Sat May 28, 2005 4:22 pm ] |
Post subject: | |
Thanks Yeah...that was pretty easy. |
Author: | wtd [ Sat May 28, 2005 5:03 pm ] | ||
Post subject: | |||
For what it's worth, my solution looks like:
|
Author: | rizzix [ Sat May 28, 2005 5:12 pm ] |
Post subject: | |
that won't work... cuz that basically means it has to start with an 'a' |
Author: | wtd [ Sat May 28, 2005 5:31 pm ] | ||
Post subject: | |||
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:
|
Author: | rizzix [ Sat May 28, 2005 5:41 pm ] |
Post subject: | |
i think it says it should start with 'a|A' or any other character... |
Author: | wtd [ Sat May 28, 2005 5:48 pm ] | ||
Post subject: | |||
How about:
|
Author: | rizzix [ Sat May 28, 2005 5:53 pm ] |
Post subject: | |
that means it has to end with an 'a|A', it will not match "aWay" |
Author: | GlobeTrotter [ Sat May 28, 2005 5:55 pm ] |
Post subject: | |
What kind of code is this stuff? I'm pretty confused. |
Author: | wtd [ Sat May 28, 2005 5:59 pm ] | ||
Post subject: | |||
rizzix wrote: that means it has to end with an 'a|A', it will not match "aWay"
Hmmm...
|
Author: | wtd [ Sat May 28, 2005 6:00 pm ] |
Post subject: | |
GlobeTrotter wrote: What kind of code is this stuff? I'm pretty confused.
We're talking in regular expressions. What are regular expressions? There are many more tutorials like that one. |
Author: | rizzix [ Sat May 28, 2005 6:07 pm ] | ||
Post subject: | |||
wtd wrote: rizzix wrote: that means it has to end with an 'a|A', it will not match "aWay"
Hmmm...
that means it will match "b" which is wrong. |
Author: | wtd [ Sat May 28, 2005 6:31 pm ] |
Post subject: | |
Ok smartypants, let's see your answer. |
Author: | rizzix [ Sat May 28, 2005 6:36 pm ] | ||
Post subject: | |||
still working on checking for at least one 'a'... |