Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Regular Expression to match alternating characters
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Martin




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: Sat May 28, 2005 3:06 pm   Post subject: (No subject)

It's pretty easy.

Match an "a" or "A" one time.

code:
/a/i


Then match any character exactly once or zero times.

code:
/a.?/


Now, make this a non-matching group. Now match that group one or more times.
Martin




PostPosted: Sat May 28, 2005 4:22 pm   Post subject: (No subject)

Thanks Smile

Yeah...that was pretty easy.
wtd




PostPosted: Sat May 28, 2005 5:03 pm   Post subject: (No subject)

For what it's worth, my solution looks like:

code:
/^(?:a.?)+$/i
rizzix




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Sat May 28, 2005 5:48 pm   Post subject: (No subject)

How about:

code:
/^(?:.?a)+$/i
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: 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




PostPosted: Sat May 28, 2005 5:55 pm   Post subject: (No subject)

What kind of code is this stuff? I'm pretty confused.
wtd




PostPosted: 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...

code:
/^(?:.?a)*.?$/i
wtd




PostPosted: 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




PostPosted: 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...

code:
/^(?:.?a)*.?$/i



that means it will match "b" which is wrong. Razz
wtd




PostPosted: Sat May 28, 2005 6:31 pm   Post subject: (No subject)

Ok smartypants, let's see your answer. Smile
rizzix




PostPosted: Sat May 28, 2005 6:36 pm   Post subject: (No subject)

PerlRegex:
/^(?:.?a)+.?$/i


still working on checking for at least one 'a'... Razz
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: