Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Regex-tut] Sets: Ranges and Negatives
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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:

code:
[a-z]


It works with capital letters and numbers too, and multiple ranges can be specified in the same set:

code:
[a-zA-Z0-9]


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:

code:
/(['"])(.*)\1/


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:

code:
[^ \1 ]


Matching anything that isn't whatever the first group matched.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: