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

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




PostPosted: Tue Nov 16, 2004 11:45 pm   Post subject: [Regex-tut] Case insensitivity and local modifiers

Quick recap

In previous tutorials I used a set to achieve case insensitivity at the beginning on a word.

code:
/[hH]ello/


It works, but...

This works, and it's even fairly concise, but, it doesn't really indicate that case insensitivity was my goal. It also gets messier if, for instance, I wanted to make the entire word "hello" case-insensitive.

code:
/[hH][eE][lL][lL][oO]/


There's a better way?

Yes, there is. The "i" modifier can make the entire regular expression case-insensitive. It's applied in the same way as the m and x modifiers.

code:
/hello/i


Back to the future

Going back to the original example, we want only the first letter of "hello" to be case-insensitive. Fortunately, we can apply modifiers locally.

code:
/(?i:h)ello/


Similarly, modifiers can be negated locally. A rather odd reversal of the above might look like:

code:
/h(?-1:ello)/i


These two styles can be mixed.

code:
/(?i-x:h) ello /x
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: