| | |
Regular expression doesn't make sense
![]() |
•
•
Join Date: Oct 2006
Posts: 95
Reputation:
Solved Threads: 3
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
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
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:
prints:
/^[^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)
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'); for (@strings) { /^[^TMC]ow/ && print "$_\n"; }
prints:
Perl Syntax (Toggle Plain Text)
Bow tie Pow wow 9ow begins with a digit ow begins with a space
Last edited by KevinADC; Oct 28th, 2006 at 3:05 pm.
![]() |
Similar Threads
- backreferencing in Perl 5.005_2 (Perl)
- Regular Expression (JSP)
- regular expression (PHP)
- Regular Expression Matching New Line (Perl)
- Regular expression (Perl)
- Looking for table rows with Regular expression. (PHP)
Other Threads in the Perl Forum
- Previous Thread: calling one perl program from within another
- Next Thread: CGI + custom error pages...
| Thread Tools | Search this Thread |





