tags:

views:

109

answers:

1

consider the following code:

perl -wne 'chomp;print if m/[^(?:test)]/'

I was surprised to see that grouping inside a character class works, How does this differ from (?!pattern)?

+11  A: 
/[^(?:test)]/

is not grouping within the char class. All the char listed in the [ ] after ^ will be treated literally and this will match any string that contains char other than ( ? : t e s t )

codaddict
I think one of the most common regular expression mistakes I see is [\.].
Eric
The grouping does not work within character classes. Here I can't stop myself referring to the wonderful book of Jeffrey Friedl
Necip
Shouldn't that be: ...any string that *consists of one (1) char* other than...?
Tim Pietzcker
@Tim: Since the regex is not anchored to the beginning/end of string, it can match at any point in the string and doesn't care what comes before or after it. A string with one character other than those will match, but a string with 100 will also match (100 times - once for each character other than `(?:test)`).
Dave Sherohman
@Dave Sherohman: It would only match 100 times if you used `/g`. Since he didn't, it only finds the first match on a line.
cjm
@cjm: Yes, I knew I was begin sloppy in my phrasing but didn't bother to clean it up. How's this? "There are 100 places where it could potentially match, even though only one of these matches will be selected and returned." That input would *contain* 100 matching characters, regardless of whether only one or all 100 are *returned*.
Dave Sherohman