943,998 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 1977
  • Perl RSS
Oct 28th, 2006
0

Regular expression doesn't make sense

Expand Post »
Hi, I'm studying for my CIW perl exam, I've come across this question in my exam software, but it doesn't make sense, and my solution is different to the solution given.

Here is the question:

The following RE matches what patterns?
/[[^TMC]ow/

Not Mow
Mow at the beginning of a string
Not Cow
Cow at the beginning of a string
Tow at the beginning of a string
Not Tow


The given solution is 'Not Mow,Not Cow,Not Tow', but when I run it, I get all of them as matches... Its the extra [ that I don't understand, can anyone explain whats going on. :rolleyes:

Thanks very much, I am very grateful for your help
Similar Threads
Reputation Points: 20
Solved Threads: 4
Junior Poster
cms271828 is offline Offline
123 posts
since Oct 2006
Oct 28th, 2006
0

Re: Regular expression doesn't make sense

Not sure where you got that regexp from but it's either a typographical error or just wrong. The first '[' is doing nothing unless it's literally part of the search pattern. It's also not tied to the beginning of the string. The '^' is the beginning of string anchor but not when it is inside of a character class: []. When it's inside the character class it means to not match what's in the character class (a negated character class). Maybe it was supposed to be:

/^[^TMC]ow/

in which case it means to match a string that begins with any character followed by 'ow' but not if the character is T or M or C:

Perl Syntax (Toggle Plain Text)
  1. my @strings = ('Cows say moo', 'I Mow the grass', 'Tow the line', 'Bow tie', 'Pow wow', '9ow begins with a digit', ' ow begins with a space');
  2.  
  3. for (@strings) {
  4. /^[^TMC]ow/ && print "$_\n";
  5. }

prints:

Perl Syntax (Toggle Plain Text)
  1. Bow tie
  2. Pow wow
  3. 9ow begins with a digit
  4. ow begins with a space
Last edited by KevinADC; Oct 28th, 2006 at 3:05 pm.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: calling one perl program from within another
Next Thread in Perl Forum Timeline: CGI + custom error pages...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC