On Sat 12 May 2007 at 11:11:33 +0000, Duncan wrote: > [] indicates a character class. Any of the enumerated characters will > match. A range may be indicated with a dash (which can be matched > literally by placing it first, after a ^ if any), and a ^ as the first > character negates. As with a -, a ] must be placed first (or escaped) as
This (about characters being escaped within []) is a widespread misunderstanding, but it is Not True(tm). aside: Worse, some people who re-implemented regular expression matchers also misunderstood it, making even more of a mess, because it is now "sometimes true, sometimes false". Regular expressions in Perl have to follow some Perl rules too, so the "true" regular expression sometimes needs an extra backslash just so that Perl understands where it starts and ends... creating even more confusion. I don't know how that is handled in the "pcre" library. Within [] (called a character class) all characters lose their other meaning and only some characters have special meaning: ^ at the start for negation - for a range ] to end the character class. that is all (apart from the abomination of POSIX character classes which was invented much later than the rest, and which complicates matters needlessly). To include a - literally, use it at the start or end (because it would not be a complete range specification). To include a ] literally, use it at the start (because it would make the character class empty and thus useless). So [^]-] is a character class matching all characters except ] and -, and [][] is a character class matching ] and [ and []^-] is a character class matching ] ^ and -. >From the re_format(7) manual page which deals with the POSIX character classes too (you don't really want to know about them unless there is no other way): A bracket expression is a list of characters enclosed in `[]'. It nor- mally matches any single character from the list (but see below). If the list begins with `^', it matches any single character (but see below) not from the rest of the list. If two characters in the list are separated by `-', this is shorthand for the full range of charac- ters between those two (inclusive) in the collating sequence, e.g. `[0-9]' in ASCII matches any decimal digit. It is illegal- for two ranges to share an endpoint, e.g. `a-c-e'. Ranges are very collating- sequence-dependent, and portable programs should avoid relying on them. To include a literal `]' in the list, make it the first character (fol- lowing a possible `^'). To include a literal `-', make it the first or last character, or the second endpoint of a range. To use a literal `-' as the first endpoint of a range, enclose it in `[.' and `.]' to make it a collating element (see below). With the exception of these and some combinations using `[' (see next paragraphs), all other spe- cial characters, including `\', lose their special significance within a bracket expression. Within a bracket expression, a collating element (a character, a multi- character sequence that collates as if it were a single character, or a collating-sequence name for either) enclosed in `[.' and `.]' stands for the sequence of characters of that collating element. The sequence is a single element of the bracket expression's list. A bracket expression containing a multi-character collating element can thus match more than one character, e.g. if the collating sequence includes a `ch' collating element, then the RE `[[.ch.]]*c' matches the first five characters of `chchcc'. Within a bracket expression, a collating element enclosed in `[=' and `=]' is an equivalence class, standing for the sequences of characters of all collating elements equivalent to that one, including itself. (If there are no other equivalent collating elements, the treatment is as if the enclosing delimiters were `[.' and `.]'.) For example, if o and ^ are the members of an equivalence class, then `[[=o=]]', `[[=^=]]', and `[o^]' are all synonymous. An equivalence class may not- be an endpoint of a range. Within a bracket expression, the name of a character class enclosed in `[:' and `:]' stands for the list of all characters belonging to that class. Standard character class names are: alnum digit punct alpha graph space blank lower upper cntrl print xdigit These stand for the character classes defined in ctype(3). A locale may provide others. A character class may not be used as an endpoint of a range. There are two special cases- of bracket expressions: the bracket expressions `[[:<:]]' and `[[:>:]]' match the null string at the begin- ning and end of a word respectively. A word is defined as a sequence of word characters which is neither preceded nor followed by word char- acters. A word character is an alnum character (as defined by ctype(3)) or an underscore. This is an extension, compatible with but not specified by POSIX 1003.2, and should be used with caution in soft- ware intended to be portable to other systems. -Olaf. -- ___ Olaf 'Rhialto' Seibert -- You author it, and I'll reader it. \X/ rhialto/at/xs4all.nl -- Cetero censeo "authored" delendum esse. _______________________________________________ Pan-users mailing list Pan-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/pan-users