Author |
Message |
Martin
|
Posted: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Sat May 28, 2005 3:06 pm Post subject: (No 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. |
|
|
|
|
|
Martin
|
Posted: Sat May 28, 2005 4:22 pm Post subject: (No subject) |
|
|
Thanks
Yeah...that was pretty easy. |
|
|
|
|
|
wtd
|
Posted: Sat May 28, 2005 5:03 pm Post subject: (No subject) |
|
|
For what it's worth, my solution looks like:
|
|
|
|
|
|
rizzix
|
Posted: Sat May 28, 2005 5:12 pm Post subject: (No subject) |
|
|
that won't work... cuz that basically means it has to start with an 'a' |
|
|
|
|
|
wtd
|
Posted: Sat May 28, 2005 5:31 pm Post subject: (No 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:
code: | success = str =~ /^(?:a.?)+$/i && str =~ /a/ |
|
|
|
|
|
|
rizzix
|
Posted: Sat May 28, 2005 5:41 pm Post subject: (No subject) |
|
|
i think it says it should start with 'a|A' or any other character... |
|
|
|
|
|
wtd
|
Posted: Sat May 28, 2005 5:48 pm Post subject: (No subject) |
|
|
How about:
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Sat May 28, 2005 5:53 pm Post subject: (No subject) |
|
|
that means it has to end with an 'a|A', it will not match "aWay" |
|
|
|
|
|
GlobeTrotter
|
Posted: Sat May 28, 2005 5:55 pm Post subject: (No subject) |
|
|
What kind of code is this stuff? I'm pretty confused. |
|
|
|
|
|
wtd
|
Posted: Sat May 28, 2005 5:59 pm Post subject: (No subject) |
|
|
rizzix wrote: that means it has to end with an 'a|A', it will not match "aWay"
Hmmm...
|
|
|
|
|
|
wtd
|
Posted: Sat May 28, 2005 6:00 pm Post subject: (No 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. |
|
|
|
|
|
rizzix
|
Posted: Sat May 28, 2005 6:07 pm Post subject: (No 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. |
|
|
|
|
|
wtd
|
Posted: Sat May 28, 2005 6:31 pm Post subject: (No subject) |
|
|
Ok smartypants, let's see your answer. |
|
|
|
|
|
rizzix
|
Posted: Sat May 28, 2005 6:36 pm Post subject: (No subject) |
|
|
PerlRegex: | /^(?:.?a)+.?$/i |
still working on checking for at least one 'a'... |
|
|
|
|
|
|