wtd
|
Posted: Sat Nov 13, 2004 11:43 pm Post subject: [Regex-tut] Sets: Ranges and Negatives |
|
|
In the distant past
I mentioned sets earlier. I used one to match either "h" or "H".
Imagine matching any letter of the alphabet.
code: | [abcdefghijklmnopqrstuvwxyz] |
It isn't pretty or even concise.
Ranges to the rescue
It will reassure anyone traumatized by that last example to know that it's quite possible to specify ranges in sets. With ranges, the above becomes the much more manageable:
It works with capital letters and numbers too, and multiple ranges can be specified in the same set:
Negative sets
Ranges are great, and they can remove a lot of work, but it'd still be a lot of work to list everything except for a certain set of characters.
Fortunately, there's an easy answer. A caret at the beginning of a set denotes a negative set, which matches anything that isn't in the set.
So, let's go back to the quote matching problem. I can already match stuff between quotes:
But, what if I don't want the stuff in between to include the same kind of quote?
code: | / ( [ ' " ] ) ( [^ \1 ]* ) \1 /x |
That'll do the trick nicely, with:
Matching anything that isn't whatever the first group matched. |
|
|